NHibernate.cfg.xml文件配置
Data Source=.;database=NHibernateSample; uid=sa;pwd=123;
//创建数据库
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 的配置:
database='nhibernate';server=.;uid='sa';pwd='sa';
----------------------------------------------------------------------------------------------------------------------
#region 2 。 App.config 配置
Configuration config = new Configuration().AddAssembly("Test.Model");
factory = config.BuildSessionFactory();
session = factory.OpenSession();
#endregion
--------------------------------------------------------------------------------------------------------------------------
2 web.config的配置 注意 所放字节位置
---------------------------------------------------------------------------------------------------------
#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