NHibernateHelper.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using NHibernate;
  5. using NHibernate.Cfg;
  6. using System.Collections;
  7. namespace NHibernateHelper
  8. {
  9. public class NHibernateHelper
  10. {
  11. private static ISessionFactory factory = null;
  12. public static ISessionFactory GetSessionFactory(string dialect, string driver, string connstr)
  13. {
  14. //读取配置文件
  15. //读取所有的映射文件--必须是嵌入资源
  16. try
  17. {
  18. Configuration config = new Configuration().Configure();//.AddAssembly("Model");
  19. config.Properties[NHibernate.Cfg.Environment.Dialect] = dialect;//"NHibernate.Dialect.OracleDialect";
  20. config.Properties[NHibernate.Cfg.Environment.ConnectionDriver] = driver; //"NHibernate.Driver.OracleClientDriver";
  21. config.Properties[NHibernate.Cfg.Environment.ConnectionString] = connstr;//"Data Source=core;Integrated Security=false;uid=cxuser;pwd=cxuser;Pooling=true;Min Pool Size=3;Max Pool Size=4";
  22. //创建Session工厂,负责持久化的连接以及OR映射,该对象的开销比较大,一般我们建议用单例模式来实现
  23. factory = config.BuildSessionFactory();
  24. return factory;
  25. }
  26. catch (Exception ex)
  27. {
  28. throw new Exception(ex.Message);
  29. }
  30. }
  31. }
  32. }