简单的连接子类示例尝试更新基表的标识列

发布于 2024-09-09 01:06:15 字数 6288 浏览 5 评论 0原文

我必须在这个概念中遗漏一些非常重要的东西才能得到这个 错误,因此任何帮助表示赞赏。

我明白了 ----> System.Data.SqlClient.SqlException:无法更新身份 列“产品ID”。

当我运行此

 var tblEvent = session.Load<TblEvent>(17);
           tblEvent.Code+= " - test update";
           using (var tran = session.BeginTransaction())
           {
               session.SaveOrUpdate(tblEvent);
               tran.Commit();
           }

负载时,负载工作正常,我可以检查所有属性,但更新失败。

这是映射文件

<hibernate-mapping namespace="Booking.BLL" assembly="Booking.BLL" xmlns="urn:nhibernate-mapping-2.2" >
 <class name="TblProduct" table="tblProduct" schema="dbo">
   <id name="_productID" access="field" column="ProductID"    type="Int32" >
     <generator class="native"  />
   </id>
   <property name="Active" type="Boolean" not-null="true" />
   <property name="AllowBackOrder" type="Boolean" not-null="true" />
   <property name="BuyY" type="Int32" precision="10" />
   <property name="BuyYQty" type="Int32" precision="10" />
   <property name="CategoryID" type="Int32" precision="10" />
   <property name="Code" type="String" length="50" />
   <property name="ColourID" type="Int32" precision="10" />
   <property name="ColourNameOverride" type="String" length="250" />
   <property name="Cost" type="Decimal" precision="10" not-null="true" />
   <property name="CreatedBy" type="Int32" precision="10" />
   <property name="CreatedDate" type="DateTime" />
   <property name="CustomSort1" type="Int32" precision="10" />
   <property name="CustomSort2" type="Int32" precision="10" />
   <property name="CustomSort3" type="Int32" precision="10" />
   <property name="DefaultImageID" type="Int32" precision="10" />
   <property name="Depth" type="Decimal" precision="10" not-null="true" />
   <property name="DispatchCategoryID" type="Int32" precision="10" />
   <property name="DisplayIfNoStock" type="Boolean" not-null="true" />
   <property name="GetXFree" type="Int32" precision="10" />
   <property name="GetXPercentDiscount" type="Decimal" precision="6" />
   <property name="Height" type="Decimal" precision="10" not-null="true" />
   <property name="IsFavourite" type="Boolean" not-null="true" />
   <property name="IsFeatured" type="Boolean" not-null="true" />
   <property name="IsInSite" type="Int32" precision="10" />
   <property name="IsSpecial" type="Boolean" not-null="true" />
   <property name="Keywords" type="String" length="250" />
   <property name="LongDescription" length="2147483647" />
   <property name="LongDescription2" length="2147483647" />
   <property name="LongDescription3" length="2147483647" />
   <property name="ManufacturerID" type="Int32" precision="10" />
   <property name="Name" type="String" length="250" not-null="true" />
   <property name="NoStockMessage" type="String" length="250" />
   <property name="OrderRanking" type="Int32" precision="10" />
   <property name="ParentID" type="Int32" precision="10" />
   <property name="Price" type="Decimal" precision="10" not-null="true" />
   <property name="PriceMember" type="Decimal" precision="10" />
   <property name="PriceRRP" type="Decimal" precision="10" />
   <property name="PriceSpecial" type="Decimal" precision="10" />
   <property name="PromotionID" type="Int32" precision="10" />
   <property name="RefExt" type="String" length="250" />
   <property name="SellBundledOnly" type="Boolean" />
   <property name="Sex" type="Int32" precision="10" />
   <property name="ShortDescription" type="String" length="500" not-null="true" />
   <property name="Size" type="String" length="50" />
   <property name="SizeID" type="Int32" precision="10" />
   <property name="SizeNameOverride" type="String" length="250" />
   <property name="SizeUnit" type="String" length="10" />
   <property name="SpecialEnd" type="DateTime" />
   <property name="SpecialStart" type="DateTime" />
   <property name="Status" type="Boolean" not-null="true" />
   <property name="Stock" type="Int32" precision="10" not-null="true" />
   <property name="SupplierID" type="Int32" precision="10" />
   <property name="TaxExempt" type="Boolean" not-null="true" />
   <property name="UpdateBy" type="Int32" precision="10" />
   <property name="UpdateDate" type="DateTime" />
   <property name="WeightKG" type="Decimal" precision="10" not-null="true" />
   <property name="Width" type="Decimal" precision="10" not-null="true" />
   <many-to-one name="Parent" class="TblProduct" column="ProductID" not-null="true" />
   <bag name="TblProducts" table="tblProduct" inverse="true">
     <key column="ProductID" />
     <one-to-many class="TblProduct" />
   </bag>
       <joined-subclass name="TblEvent" table="tblEvent">
       <key column="EventID" />
               <property name="AttachmentFileURL" type="String" length="200" />
               <property name="GLAccountNo" type="String" length="20" />
               <property name="MultiDayEvent" type="Boolean" not-null="true" />
               <property name="PromotionActive" type="Boolean" not-null="true" />
               <property name="RequiresLogin" type="Boolean" not-null="true" />
               <property name="TopLevelCategoryID" type="Int32" precision="10" not-null="true" />
               <property name="TotalAllocation" type="Int32" precision="10" not-null="true" />
               <bag name="TblSessionTemplates" table="tblSessionTemplate" inverse="true">
                 <key column="EventID" />
                 <one-to-many class="TblSessionTemplate" />
               </bag>
               <bag name="TblSessions" table="tblSession" inverse="true">
                 <key column="EventID" />
                 <one-to-many class="TblSession" />
               </bag>
       </joined-subclass>    
 </class>
</hibernate-mapping>

I must be missing something very important in the concept to get this
error so any help appreciated.

I get
----> System.Data.SqlClient.SqlException : Cannot update identity
column 'ProductID'.

when i run this

 var tblEvent = session.Load<TblEvent>(17);
           tblEvent.Code+= " - test update";
           using (var tran = session.BeginTransaction())
           {
               session.SaveOrUpdate(tblEvent);
               tran.Commit();
           }

The load works correctly and i can inspect all the properties but the update fails.

Heres the mapping file

<hibernate-mapping namespace="Booking.BLL" assembly="Booking.BLL" xmlns="urn:nhibernate-mapping-2.2" >
 <class name="TblProduct" table="tblProduct" schema="dbo">
   <id name="_productID" access="field" column="ProductID"    type="Int32" >
     <generator class="native"  />
   </id>
   <property name="Active" type="Boolean" not-null="true" />
   <property name="AllowBackOrder" type="Boolean" not-null="true" />
   <property name="BuyY" type="Int32" precision="10" />
   <property name="BuyYQty" type="Int32" precision="10" />
   <property name="CategoryID" type="Int32" precision="10" />
   <property name="Code" type="String" length="50" />
   <property name="ColourID" type="Int32" precision="10" />
   <property name="ColourNameOverride" type="String" length="250" />
   <property name="Cost" type="Decimal" precision="10" not-null="true" />
   <property name="CreatedBy" type="Int32" precision="10" />
   <property name="CreatedDate" type="DateTime" />
   <property name="CustomSort1" type="Int32" precision="10" />
   <property name="CustomSort2" type="Int32" precision="10" />
   <property name="CustomSort3" type="Int32" precision="10" />
   <property name="DefaultImageID" type="Int32" precision="10" />
   <property name="Depth" type="Decimal" precision="10" not-null="true" />
   <property name="DispatchCategoryID" type="Int32" precision="10" />
   <property name="DisplayIfNoStock" type="Boolean" not-null="true" />
   <property name="GetXFree" type="Int32" precision="10" />
   <property name="GetXPercentDiscount" type="Decimal" precision="6" />
   <property name="Height" type="Decimal" precision="10" not-null="true" />
   <property name="IsFavourite" type="Boolean" not-null="true" />
   <property name="IsFeatured" type="Boolean" not-null="true" />
   <property name="IsInSite" type="Int32" precision="10" />
   <property name="IsSpecial" type="Boolean" not-null="true" />
   <property name="Keywords" type="String" length="250" />
   <property name="LongDescription" length="2147483647" />
   <property name="LongDescription2" length="2147483647" />
   <property name="LongDescription3" length="2147483647" />
   <property name="ManufacturerID" type="Int32" precision="10" />
   <property name="Name" type="String" length="250" not-null="true" />
   <property name="NoStockMessage" type="String" length="250" />
   <property name="OrderRanking" type="Int32" precision="10" />
   <property name="ParentID" type="Int32" precision="10" />
   <property name="Price" type="Decimal" precision="10" not-null="true" />
   <property name="PriceMember" type="Decimal" precision="10" />
   <property name="PriceRRP" type="Decimal" precision="10" />
   <property name="PriceSpecial" type="Decimal" precision="10" />
   <property name="PromotionID" type="Int32" precision="10" />
   <property name="RefExt" type="String" length="250" />
   <property name="SellBundledOnly" type="Boolean" />
   <property name="Sex" type="Int32" precision="10" />
   <property name="ShortDescription" type="String" length="500" not-null="true" />
   <property name="Size" type="String" length="50" />
   <property name="SizeID" type="Int32" precision="10" />
   <property name="SizeNameOverride" type="String" length="250" />
   <property name="SizeUnit" type="String" length="10" />
   <property name="SpecialEnd" type="DateTime" />
   <property name="SpecialStart" type="DateTime" />
   <property name="Status" type="Boolean" not-null="true" />
   <property name="Stock" type="Int32" precision="10" not-null="true" />
   <property name="SupplierID" type="Int32" precision="10" />
   <property name="TaxExempt" type="Boolean" not-null="true" />
   <property name="UpdateBy" type="Int32" precision="10" />
   <property name="UpdateDate" type="DateTime" />
   <property name="WeightKG" type="Decimal" precision="10" not-null="true" />
   <property name="Width" type="Decimal" precision="10" not-null="true" />
   <many-to-one name="Parent" class="TblProduct" column="ProductID" not-null="true" />
   <bag name="TblProducts" table="tblProduct" inverse="true">
     <key column="ProductID" />
     <one-to-many class="TblProduct" />
   </bag>
       <joined-subclass name="TblEvent" table="tblEvent">
       <key column="EventID" />
               <property name="AttachmentFileURL" type="String" length="200" />
               <property name="GLAccountNo" type="String" length="20" />
               <property name="MultiDayEvent" type="Boolean" not-null="true" />
               <property name="PromotionActive" type="Boolean" not-null="true" />
               <property name="RequiresLogin" type="Boolean" not-null="true" />
               <property name="TopLevelCategoryID" type="Int32" precision="10" not-null="true" />
               <property name="TotalAllocation" type="Int32" precision="10" not-null="true" />
               <bag name="TblSessionTemplates" table="tblSessionTemplate" inverse="true">
                 <key column="EventID" />
                 <one-to-many class="TblSessionTemplate" />
               </bag>
               <bag name="TblSessions" table="tblSession" inverse="true">
                 <key column="EventID" />
                 <one-to-many class="TblSession" />
               </bag>
       </joined-subclass>    
 </class>
</hibernate-mapping>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

圈圈圆圆圈圈 2024-09-16 01:06:15

事实证明,这是指向 ProductID 而不是 ParentID 的多对一关系。
db2hbm 似乎无法处理自引用外键

Ok turns out it was the many-to-one relationship pointing to ProductID instead of parentID.
db2hbm seems to have not been able to handle self referencing foreign keys

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文