標籤:nhibernate.cfg.xml 設定檔
A. 問題:
NHibernate.cfg.xml用來幹什麼的?
答:
設定資料庫的基本資料和configuration和sessionFactory執行個體的基本資料的設定檔。
B. 如何配置NHibernate.cfg.xml檔案?
答:
-
第一種方式
Configuration config = new Configuration();
這種配置方法將會到應用程式設定檔(App.Config,Web.Config)中尋找NHibernate的配置資訊,NHibernate的配置節必須符合應用程式設定檔個格式。
下面是該種方式的一個例子:
<?xml version="1.0" encoding="utf-8" ?><configuration><!-- Add this element --><configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/></configSections><!-- Add this element --><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"><session-factory><property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property><property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property><property name="connection.connection_string">Server=TLSZ207/SQLEXPRESS;initial catalog=Test;Integrated Security=true</property></session-factory></hibernate-configuration><!-- Leave the system.web section unchanged --><system.web></system.web></configuration>
2.第二種方式
Configuration config = new Configuration().Configure();
這種配置方法將會在應用的相同目錄尋找名為”hibernate.cfg.xml”的標準Hibernate配置
<?xml version="1.0" encoding="utf-8" ?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.0" > <session-factory name="MySessionFactory"> <!-- properties --> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string">Server=localhost;initial catalog=Hibernate;Integrated Security=SSPI</property> <property name="show_sql">false</property> <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property> <property name="use_outer_join">true</property> <property name="query.substitutions">true 1, false 0, yes ‘Y‘, no ‘N‘</property> <!-- mapping files --> <mapping assembly="Test.Model" /> </session-factory></hibernate-configuration>
3.第三方式
這種配置方法將尋找指定的Hibernate標準設定檔,可以是絕對路徑或者相對路徑。還可以通過編碼的方式來添加配置資訊:Configuration config = new Configuration().Configure(NHibernate.cfg.xml設定檔路徑);
對應檔:
所有的XML映射都需要使用nhibernate-mapping-2.0 schema。目前的schema可以在NHibernate的資源路徑或者是NHibernate.dll的嵌入資源(Embedded Resource)中找到。NHibernate總是會優先使用嵌入在資源中的schema檔案。你可以將hibernate-mapping拷貝到C: /Program Files/Microsoft Visual Studio .NET 2013/Common7/Packages/schemas/xml路徑中,以獲得智能感知功能
本文出自 “愛咖啡” 部落格,請務必保留此出處http://4837471.blog.51cto.com/4827471/1564224
NHibernate.cfg.xml檔案總結