在我的模型中,我使用 bigint (ulong) 作为实体键的类型。值 0 必须用于空键。外键的列不能为空,因为在我的方法中我只想检查值 0 而不是空值。一切正常,除了删除其他实体引用的相关实体。
这是我的模型:
<cf:entity name="Customer" cfom:bindingList="false">
<!--persistenceIdentity is true, because the corresponding column for this property must be auto incremented by the database.-->
<cf:property name="Id" typeName="ulong" key="true" persistenceIdentity="true" cfps:hint="CLUSTERED" />
<cf:property name="Name" typeName="string" />
</cf:entity>
<cf:entity name="Order" cfom:bindingList="false">
<!--persistenceIdentity is true, because the corresponding column for this property must be auto incremented by the database.-->
<cf:property name="Id" typeName="ulong" key="true" persistenceIdentity="true" cfps:hint="CLUSTERED" />
<!--persistenceNullable is false, because the column for the foreign key must not be nullable.-->
<cf:property name="Customer" typeName="{0}.Customer" persistenceNullable="false" />
</cf:entity>
这是我的代码:
Customer customer = new Customer();
customer.Save();
Order order = new Order();
order.Customer = customer;
order.Save();
customer.Delete();
最后一条语句给出以下错误:无法将值 NULL 插入列“Order_Customer_Id”,表“CodeFluentTest.dbo.Order”;列不允许空值。更新失败。
这是因为 Customer_Delete 存储过程包含以下更新语句: UPDATE [Order] SET [Order].[Order_Customer_Id] = NULL WHERE ([Order].[Order_Customer_Id] = @Customer_Id)
当然这不起作用,因为 Order_Customer_Id 列不可为空。如何指示 CodeFluent 将值 0 而不是 NULL 放入 Order_Customer_Id 列?