NHibernate系列文章五:NHibernate配置

摘要

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属性

 1         public static ISessionFactory SessionFactory
 2         {
 3             get
 4             {
 5                 if (_sessionFactory == null)
 6                 {
 7                     var cfg = new Configuration();
 8                     cfg.Configure();
 9                     _sessionFactory = cfg.BuildSessionFactory();
10                 }
11                 return _sessionFactory;
12             }
13         }

去掉了语句cfg.DataBaseIntegration和cfg.AddAssembly(Assembly.GetExecutingAssembly())。用cfg.Configure()替代,省去了不少代码。

F5执行,得到运行结果。

 3. 代码和xml混合配置

 修改SessionFactory

 1         public static ISessionFactory SessionFactory
 2         {
 3             get
 4             {
 5                 if (_sessionFactory == null)
 6                 {
 7                     var cfg = new Configuration();
 8                     cfg.DataBaseIntegration(x =>
 9                     {
10                         x.LogSqlInConsole = true;
11                     });
12                     cfg.Configure();
13                     _sessionFactory = cfg.BuildSessionFactory();
14                 }
15                 return _sessionFactory;
16             }
17         }

还是是通过cfg.DataBaseIntegration方法,传入委托参数,设置LogSqlInConsole参数。

F5执行,得到结果:

也将该配置放在hibernate.cfg.xml文件内。

修改hibernate.cfg.xml文件

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
 3   <session-factory>
 4     <property name="connection.connection_string_name">default</property>
 5     <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
 6     <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
 7     <property name="show_sql">true</property>
 8     <mapping assembly="NHibernateDemoApp"/>
 9   </session-factory>
10 </hibernate-configuration>

在hibernate.cfg.xml文件内对应的显示sql执行语句的属性是show_sql。

同时,在SessionFactory属性内,注释掉cfg.DataBaseIntegration调用。

 1         public static ISessionFactory SessionFactory
 2         {
 3             get
 4             {
 5                 if (_sessionFactory == null)
 6                 {
 7                     var cfg = new Configuration();
 8                     //cfg.DataBaseIntegration(x =>
 9                     //{
10                     //    x.LogSqlInConsole = true;
11                     //});
12                     cfg.Configure();
13                     _sessionFactory = cfg.BuildSessionFactory();
14                 }
15                 return _sessionFactory;
16             }
17         }

再次F5执行,得到显示sql语句的运行结果。

一般不建议使用混合方式进行配置,因为如果代码和xml同时对相同的属性进行了配置,将产生无法预计的结果。 

原文地址:https://www.cnblogs.com/uncle_danny/p/5635123.html