【问题标题】:NHibernate list index mapping to non nullable database columnNHibernate 列表索引映射到不可为空的数据库列
【发布时间】:2012-06-15 11:20:01
【问题描述】:

我正在尝试使用 NHibernate 映射一个列表,该列表是父子关系的一部分。数据库结构为:

CREATE TABLE [dbo].[salesvouchers](
[ID] [int] IDENTITY(1,1) NOT NULL,
[id_vouchertype] [int] NOT NULL,
[id_salespoint] [int] NOT NULL) /*and a couple more fields*/
CREATE TABLE [dbo].[salesvouchersitems](
[ID] [int] IDENTITY(1,1) NOT NULL,
[id_salesvoucher] [int] NOT NULL,
[itemposition] [int] NOT NULL)

两个表都通过具有外键的 id_salesvoucher 列相关联。问题是 itemposition 列是 SalesVoucherItem 列表的索引。将此映射文件用于 salesvoucher 类:

<class name="SalesVoucher" table="salesvouchers">
      <id name="Id" column="id">
         <generator class="native" />
      </id>
      <list name="Items" table="salesvouchersitems" lazy="true" cascade="all-delete-orphan">
         <key column="id_salesvoucher"/>
         <index column="itemposition"/>
         <one-to-many class="SalesVoucherItem"/>
      </list>
   <!--and more fields not relevant here-->
   </class>

这似乎很好,但是在进行测试以将新的 SalesVoucher 类插入到列表中的几个 SalesVoucherItem 的数据库中时,它会引发 SQL 错误: "无法在 itemposition 列中插入 NULL。列不接受空值"

似乎 NH 试图在该位置插入 NULL,这对我来说毫无意义,因为列表中的索引是强制性数据,应该在数据库中声明为这样。 有任何想法吗?谢谢您的帮助!问候。

【问题讨论】:

    标签: c# nhibernate list indexing


    【解决方案1】:

    您必须将 itemposition 列公开为属性并设置其值。 NHibernate 无法为您做到这一点。

    【讨论】:

      猜你喜欢
      • 2010-11-22
      • 2023-02-11
      • 1970-01-01
      • 2020-06-21
      • 2011-01-05
      • 2012-03-26
      • 1970-01-01
      • 2011-04-13
      • 2012-11-20
      相关资源
      最近更新 更多