NHibernate 列表索引映射到不可为空的数据库列

本文关键字:数据库 列表 索引 映射 NHibernate | 更新日期: 2023-09-27 18:36:59

我正在尝试使用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列相关联。问题是项目位置列旨在成为销售凭证项列表的索引。将此映射文件用于销售凭证类:

<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错误:"无法将 NULL 插入到项目位置列中。列不接受空值"

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

NHibernate 列表索引映射到不可为空的数据库列

必须将 itemposition 列公开为属性并设置其值。NHibernate不能为你做到这一点。