摘要

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同時對相同的屬性進行了配置,將產生無法預計的結果。