[NHibernate]第一個NHibernate的應用配置

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

  NHibernate是.Net平台下一個成熟的,開源的對象關係映射器(ORM)。本文來介紹第一次使用NHibernate的時候的配置。

  1.下載NHibernate。Nhibernate官網最新版本為NH3.3.3,:http://nhforge.org/

  2.NHibernate壓縮包解壓後,將其中Required_Bins檔案夾下的引用拷貝到你的項目根目錄下。確保包含如下檔案:

  

  3.將NHibernate.dll添加引用到你所對應的項目中。

  

  4.添加Mapping檔案。該檔案是一個將你定義的實體和資料庫表欄位進行匹配的xml檔案。一般以*.hbm.xml命名。

  5.對Mapping xml檔案添加Schema。路徑為Request_Bins檔案夾中的nhibernate-mapping.xsd。步驟:XML->Schemas->Add

  

  添加Shema後就會有對應的智能提示。

  

  

<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"                   assembly="NHibernate.Entity"                   namespace="NHibernate.Entity">  <class name="Product" table="ProductInfo" >    <id name="Id">      <generator class="guid" />    </id>    <property name="Name" />    <property name="Category" />    <property name="Discontinued" />  </class></hibernate-mapping>
View Code

  6.將Mapping檔案的Build Action設定為Embedded Resource,Copy to OutPut Directory設定為Copy always。至此一個簡單的Mapping檔案就配置好了。

  

  7.配置NHibernate資料庫檔案。一般以hibernate.cfg.xml命名,起鬨<mapping>結點指向的是你的mapping檔案的assembly name。和上面的Mapping一樣可以為hibernate.cfg.xml添加Schema,路徑路徑為Request_Bins檔案夾中的nhibernate-configuration.xsd。

   下面代碼以SQL Server2008作為Provider。

 

<?xml version="1.0" encoding="utf-8" ?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">  <session-factory>    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>    <property name="connection.connection_string">Server=.;database=tempdb;uid=***;pwd=***;</property>    <!--開發過程中用於調試-->    <property name="show_sql">true</property>    <mapping assembly="NHibernate.Entity"/>  </session-factory></hibernate-configuration>

  8.同樣也要講hibernate.cfg.xml檔案的Build Action設定為Embedded Resource,Copy to OutPut Directory設定為Copy always。至此,一個NHibernate的簡單配置就完成了。

 

轉載請註明來源:http://www.cnblogs.com/caoming/p/4154373.html

[NHibernate]第一個NHibernate的應用配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.