Nhibernate實戰(一)
先在通過PoweDesign對DB做設計。

以下是使用的經過﹕
1.撰寫Nhibernamte的ORM(object relation mapping)對象﹑關系數據庫的映射﹐
<?xml version="1.0" encoding="utf-8" ?>
<hiberate-mapping xmlns="urn:nhibernate-mapping-2.2">
<!--表示完整的類別﹐含assembly及namespace-->
<class name="Library.Entity.UserType,Library.Entity" table="UserType">
<id name="UserTypeID" column="UserTypeID" type="int" unsave-value="null">
<generator class="int" />
<!--自動產生-->
</id>
<!--若name與數據庫中的column一致﹐可省約-->
<property name="TypeName" column="TypeName" type="System.string" length="50"/>
</class>
</hiberate-mapping>
一個數據使用聯合主鍵時
<composite-id>
<key-many-to-one name="UserID" class="Library.Entity.User" column="UserID"/>
<key-one-to-one name="BookID" class="Library.Entity.Book" column="BookID"/>
</composite-id>
一對一(one-to-one)的關聯
<one-to-one name="UserTypeInfo" class="Library.Entity.UserType" />
一對多(one-to-many)的關聯
<set name="UserInfo">
<key column="UserTypeID"/>
<one-to-many class="Library.Entity.User"/>
</set>
多對一(many-to-one)的關聯
多對多(many-to-many)的關聯關系﹐一般使用三個數據表
以下是使用的經過﹕
1.撰寫Nhibernamte的ORM(object relation mapping)對象﹑關系數據庫的映射﹐
<?xml version="1.0" encoding="utf-8" ?>
<hiberate-mapping xmlns="urn:nhibernate-mapping-2.2">
<!--表示完整的類別﹐含assembly及namespace-->
<class name="Library.Entity.UserType,Library.Entity" table="UserType">
<id name="UserTypeID" column="UserTypeID" type="int" unsave-value="null">
<generator class="int" />
<!--自動產生-->
</id>
<!--若name與數據庫中的column一致﹐可省約-->
<property name="TypeName" column="TypeName" type="System.string" length="50"/>
</class>
</hiberate-mapping>
一個數據使用聯合主鍵時
<composite-id>
<key-many-to-one name="UserID" class="Library.Entity.User" column="UserID"/>
<key-one-to-one name="BookID" class="Library.Entity.Book" column="BookID"/>
</composite-id>
一對一(one-to-one)的關聯
<one-to-one name="UserTypeInfo" class="Library.Entity.UserType" />
一對多(one-to-many)的關聯
<set name="UserInfo">
<key column="UserTypeID"/>
<one-to-many class="Library.Entity.User"/>
</set>
多對一(many-to-one)的關聯
多對多(many-to-many)的關聯關系﹐一般使用三個數據表
浙公网安备 33010602011771号