摘要
NHibernate有多种配置方法,代码,xml文件,以及Fluent NHibernate。这里只介绍最常用的两种NHibernate配置方法:通过代码和通过配置文件。
1. 通过代码配置
通过代码配置就是前面文章里代码那样,调用cfg.DataBaseIntegration方法,传入委托表达式,配置连接字符串等信息。
2. 通过XML文件配置
在工程根目录下添加文件hibernate.cfg.xml
该文件中输入代码:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.connection_string_name">default</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<mapping assembly="NHibernateDemoApp"/>
</session-factory>
</hibernate-configuration>
将配置信息移到了hibernate.cfg.xml内。
修改hibernate.cfg.xml文件的属性Build Action和Copy to Output Directory。
在工程根目录下找到文件App.config,在configuration下面插入connectionStrings节。
数据库连接字符串在App.config内由”default”属性定义,而在hibernate.cfg.xml内用<property name=”connection.connection_string_name”>default</property>指向App.config内定义的default连接字符串。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="default" connectionString="Data Source=localhost;Initial Catalog=NHibernateDemoDB;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/>
</connectionStrings>
</configuration>
Asp.net web应用程序,在web.config文件中定义字符串连接字符串connectionStrings,然后在hibernate.cfg.xml文件中用connection.connection_string_name进行指向。
修改SessionFactory属性
public static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var cfg = new Configuration();
cfg.Configure();
_sessionFactory = cfg.BuildSessionFactory();
}
return _sessionFactory;
}
}
去掉了语句cfg.DataBaseIntegration和cfg.AddAssembly(Assembly.GetExecutingAssembly())。用cfg.Configure()替代,省去了不少代码。
F5执行,得到运行结果。
3. 代码和xml混合配置
修改SessionFactory
public static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var cfg = new Configuration();
cfg.DataBaseIntegration(x =>
{
x.LogSqlInConsole = true;
});
cfg.Configure();
_sessionFactory = cfg.BuildSessionFactory();
}
return _sessionFactory;
}
}
还是是通过cfg.DataBaseIntegration方法,传入委托参数,设置LogSqlInConsole参数。
F5执行,得到结果:
也将该配置放在hibernate.cfg.xml文件内。
修改hibernate.cfg.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.connection_string_name">default</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="show_sql">true</property>
<mapping assembly="NHibernateDemoApp"/>
</session-factory>
</hibernate-configuration>
在hibernate.cfg.xml文件内对应的显示sql执行语句的属性是show_sql。
同时,在SessionFactory属性内,注释掉cfg.DataBaseIntegration调用。
public static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var cfg = new Configuration();
//cfg.DataBaseIntegration(x =>
//{
// x.LogSqlInConsole = true;
//});
cfg.Configure();
_sessionFactory = cfg.BuildSessionFactory();
}
return _sessionFactory;
}
}
再次F5执行,得到显示sql语句的运行结果。
一般不建议使用混合方式进行配置,因为如果代码和xml同时对相同的属性进行了配置,将产生无法预计的结果。