NHibernate.cfg.xml文件配置示例

作者:袖梨 2022-06-25

NHibernate.cfg.xml文件配置




NHibernate.Driver.SqlClientDriver

     Data Source=.;database=NHibernateSample; uid=sa;pwd=123;

10
true
NHibernate.Dialect.MsSql2000Dialect
true
10
true1, false0, yes 'Y', no 'N'



//创建数据库
publicclass DataBase
   {
privatevoid CreateDatabase()
       {
           Configuration cfg =new Configuration();
           cfg.Configure();
           SchemaExport schema =new SchemaExport(cfg);
//schema.Drop(true, false);
           schema.SetOutputFile("log.txt");
           schema.Create(true, false);
        }
    }




Nhibernate 另外几种配置方式

1 App.config 的配置:




   




   
      NHibernate.Connection.DriverConnectionProvider,NHibernate
      NHibernate.Driver.SqlClientDriver
     
       
        database='nhibernate';server=.;uid='sa';pwd='sa';
     

      10
      true
      NHibernate.Dialect.MsSql2005Dialect
      NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
   



----------------------------------------------------------------------------------------------------------------------

#region 2 。 App.config 配置
            Configuration config = new Configuration().AddAssembly("Test.Model");        

        factory = config.BuildSessionFactory();
          session = factory.OpenSession();
#endregion

--------------------------------------------------------------------------------------------------------------------------


2 web.config的配置 注意 所放字节位置


  

  



  
    NHibernate.Connection.DriverConnectionProvider
    NHibernate.Driver.SqlClientDriver
    NHibernate.Dialect.MsSql2005Dialect
    database='nhibernate';server=.;uid='sa';pwd='sa';
    true
    NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
  





---------------------------------------------------------------------------------------------------------

 

#region 2、利用web.config 配置 通过

        //NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration().AddAssembly("Test.Model");
        //factory = config.BuildSessionFactory();
        //session = factory.OpenSession();

        #endregion

-----------------------------------------------------------------------------------------------------------

3     创建一个MyConfiguration.cs 结构如下:

 

public static NHibernate.Cfg.Configuration GetConfig()
    {

      
        NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
        config.SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
        config.SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
        config.SetProperty("hibernate.connection.connection_string", "database='nhibernate';server=.;uid='sa';pwd='sa';");
        config.SetProperty("hibernate.dialect", "NHibernate.Dialect.MsSql2005Dialect,NHibernate");
        config.SetProperty("hibernate.use_outer_join", "true");
        config.SetProperty("hibernate.show_sql", "false");
        config.SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu");
        config.AddAssembly("Test.Model");
        return config;
     
    }

 

------------------------------------------------------------------------------------------------------------------------------

#region 3、使用MyConfiguration.cs配置

        //NHibernate.Cfg.Configuration config = MyConfiguration.GetConfig();//使用MyConfiguration.cs
        //factory = config.BuildSessionFactory();
        factory = MyConfiguration.GetConfig().BuildSessionFactory();
        session = factory.OpenSession();
        #endregion

相关文章

精彩推荐