1. 程式人生 > >NHibernate 非自增主鍵與自增主鍵的對映檔案配置

NHibernate 非自增主鍵與自增主鍵的對映檔案配置

在用NHibernate的時候, 當有表中的主鍵是資料庫自動生成的時候(即設定的自增)將使用native:<generator class="native"></generator>
而當是自己填寫的時候則需要改為assigned:<generator class="assigned"></generator>
否則將會出現異常! 以上是對映檔案的配置中需要注意的

NHibernate 非自增主鍵:

<?xml version="1.0"  encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
	<class name="IWOMTracker.Data.Model.SEC_ItemAll, IWOMTracker.Data.Model" table="SEC_ItemAll">
		<id name="Item_ID" column="Item_ID" type="System.Int32"  unsaved-value="0">
      <generator class="assigned" />  (備註:非自增主鍵應該配置為assigned屬性)     			
    </id>
		<property name="Task_ID" column="Task_ID" type="System.Int32"  />
		<property name="Brand_ID" column="Brand_ID" type="System.Int32"  />
		<property name="Item_URL" column="Item_URL" type="System.String"  />
		<property name="Item_URLKey" column="Item_URLKey" type="System.String"  />
		<property name="Item_Title" column="Item_Title" type="System.String"  />
		<property name="Item_Content" column="Item_Content" type="System.String"  />
		<property name="Item_SDate" column="Item_SDate" type="System.DateTime"  />
		<property name="Item_CDate" column="Item_CDate" type="System.DateTime"  />
		<property name="Item_State" column="Item_State" type="System.Int32"  />
		<property name="Item_UDate" column="Item_UDate" type="System.DateTime"  />
		<property name="Admin_ID" column="Admin_ID" type="System.Int32"  />
		<property name="Item_ReleaseDate" column="Item_ReleaseDate" type="System.DateTime"  />
		<property name="Item_Author" column="Item_Author" type="System.String"  />
		<property name="Item_Keyword" column="Item_Keyword" type="System.String"  />
		<property name="Item_ReplyCount" column="Item_ReplyCount" type="System.Int32"  />
		<property name="Item_VisitCount" column="Item_VisitCount" type="System.Int32"  />
		<property name="Task_KeyWord" column="Task_KeyWord" type="System.String"  />
		<property name="SearchEngine_ID" column="SearchEngine_ID" type="System.Int32"  />
		<property name="Item_Info" column="Item_Info" type="System.String"  />
		<property name="Item_IsArtificial" column="Item_IsArtificial" type="System.Int32"  />
		<property name="Item_Attitude" column="Item_Attitude" type="System.Int32"  />
		<property name="Item_SiteName" column="Item_SiteName" type="System.String"  />
		<property name="Item_Inspection" column="Item_Inspection" type="System.Int32"  />
		<property name="Item_Area" column="Item_Area" type="System.String"  />
		<property name="Remark" column="Remark" type="System.String"  />
		<property name="PickupType" column="PickupType" type="System.Int32"  />
	</class>
</hibernate-mapping>

NHibernate 增主鍵:

<?xml version="1.0"  encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
	<class name="IWOMTracker.Data.Model.SEC_ItemTwoMonth, IWOMTracker.Data.Model" table="SEC_ItemTwoMonth">
		<id name="Item_ID" column="Item_ID" type="System.Int32" unsaved-value="0">
      <generator class="native" />  (備註:非自增主鍵應該配置為native屬性,並且在sql server中設定為主鍵+自增長。不然會報錯:undefine null)     			
    </id>
		<property name="Task_ID" column="Task_ID" type="System.Int32"  />
		<property name="Brand_ID" column="Brand_ID" type="System.Int32"  />
		<property name="Item_URL" column="Item_URL" type="System.String"  />
		<property name="Item_URLKey" column="Item_URLKey" type="System.String"  />
		<property name="Item_Title" column="Item_Title" type="System.String"  />
		<property name="Item_Content" column="Item_Content" type="System.String"  />
		<property name="Item_SDate" column="Item_SDate" type="System.DateTime"  />
		<property name="Item_CDate" column="Item_CDate" type="System.DateTime"  />
		<property name="Item_State" column="Item_State" type="System.Int32"  />
		<property name="Item_UDate" column="Item_UDate" type="System.DateTime"  />
		<property name="Admin_ID" column="Admin_ID" type="System.Int32"  />
		<property name="Item_ReleaseDate" column="Item_ReleaseDate" type="System.DateTime"  />
		<property name="Item_Author" column="Item_Author" type="System.String"  />
		<property name="Item_Keyword" column="Item_Keyword" type="System.String"  />
		<property name="Item_ReplyCount" column="Item_ReplyCount" type="System.Int32"  />
		<property name="Item_VisitCount" column="Item_VisitCount" type="System.Int32"  />
		<property name="Task_KeyWord" column="Task_KeyWord" type="System.String"  />
		<property name="SearchEngine_ID" column="SearchEngine_ID" type="System.Int32"  />
		<property name="Item_Info" column="Item_Info" type="System.String"  />
		<property name="Item_IsArtificial" column="Item_IsArtificial" type="System.Int32"  />
		<property name="Item_Attitude" column="Item_Attitude" type="System.Int32"  />
		<property name="Item_SiteName" column="Item_SiteName" type="System.String"  />
		<property name="Item_Inspection" column="Item_Inspection" type="System.Int32"  />
		<property name="Item_Area" column="Item_Area" type="System.String"  />
		<property name="Remark" column="Remark" type="System.String"  />
		<property name="PickupType" column="PickupType" type="System.Int32"  />
	</class>
</hibernate-mapping>

自己要看一下,NHibernate欄位的屬性設定了。