旧地址:http://blog.canself.com/nhibernate_conn/
此文仅介绍NHibernate连接数据库配置;有关NHibernate的介绍此处不做详解,具体见NHibernate资料汇总页。
想用NHibernate,首先得连接数据库,下面就看看NHibernate的数据库连接。
关于不同数据库连接的简单配置,其实官方提供了许多【官方包中的Configuration Templates】-学习之初可以使用。
步骤【一般处理】:
1、添加hibernate.cfg.xml;
注:记得修改文件的属性“复制到输出目录”,改为始终复制或较新复制
2、添加文件内容,代码如下:【下为sqlite数据库配置】
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | <?xml version="1.0" encoding="utf-8" ?><hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
 
 <session-factory name="MySessionFactory">
 <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
 <property name="connection.connection_string">Data Source=nhibernate.db;Password=12345678</property>
 <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
 </session-factory>
 
 </hibernate-configuration>
 
 | 
3、添加cs代码
| 12
 3
 
 | ISessionFactory sessionFactory = (new Configuration()).Configure().BuildSessionFactory();sessionFactory.OpenSession();
 sessionFactory.Close();
 
 | 
执行即可【注:sqlite中open不验证密码,此处可能无法验证其连接字符串正确性,可以换其他数据库尝试】
关于数据库的配置,不仅仅可以在hibernate.cfg.xml中配置数据库连接,亦可以在app.config中配置,代码如下:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | <?xml version="1.0" encoding="utf-8"?>
 <configuration>
 
 <configSections>
 <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
 </configSections>
 
 <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
 <session-factory name="MySessionFactory">
 <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
 <property name="connection.connection_string">Data Source=nhibernate.db;Password=12345678</property>
 <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
 </session-factory>
 </hibernate-configuration>
 
 <startup>
 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
 </startup>
 </configuration>
 
 | 
同样,可以使用hibernate.cfg.xml和app.config同时配置,相同的项会使用hibernate.cfg.xml配置会覆盖app.config的配置。
实际使用中,有时会出现需要连接多个数据库,可以通过手动指定配置文件来加载数据库连接,如下:【我们添加了一个second.cfg.xml文件,处理与hibernate.cfg.xml处理类似】
| 12
 3
 
 | ISessionFactory secondsessionFactory= (new Configuration()).Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "second.cfg.xml")).BuildSessionFactory();secondsessionFactory.OpenSession();
 secondsessionFactory.Close();
 
 | 
好了,简单连接配置这么多应该都用了。
下面看看hibernate.cfg.xml中的每一项的配置意义。【NHibernate.Cfg.Environment中列举了许多】
首先是上面用到的
| — | — | 
| connection.driver_class | 连接驱动【某些数据库连接可省,有dialect即可】 | 
| connection.connection_string | 数据库连接字符串 | 
| dialect | sql方言【自己意会啥意思吧】 | 
具体有哪些sql方言,直接上官网去看吧,挺多的。
下面是一些比较常用的配置:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | <property name="adonet.batch_size" >1</property>
 
 <property name="hbm2ddl.auto">
 update
 
 
 
 
 </property>
 
 <property name="max_fetch_depth">1</property>
 
 <property name="show_sql">true</property>
 
 <property name="query.substitutions">true 1,false 0</property>
 
 <property name="cache.use_query_cache">true</property>
 
 <property name="cache.use_second_level_cache">true</property>
 
 | 
其中有一个没有加进去,proxyfactory.factory_class——因这个我网上查了许多,一开始就是不知道他是干哈的,最后才有所理解,其实际知识点是“动态代理”,基础不行,后面再好好看看这一块。