標籤: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的應用配置