NHibernate.cfg.xml檔案總結

來源:互聯網
上載者:User

標籤:nhibernate.cfg.xml   設定檔   

   A. 問題:

    NHibernate.cfg.xml用來幹什麼的?


    答:

        設定資料庫的基本資料和configuration和sessionFactory執行個體的基本資料的設定檔。

    

   B. 如何配置NHibernate.cfg.xml檔案?


    答:

  1. 第一種方式

    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檔案總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.