插入时排除列
如果对象属性为 null 或其他特定值,有什么方法可以在 Fluent NHibernate 中指定在插入记录时省略一列?巧合的是,我想省略的列是我的映射中的参考。这是我的场景:
public class OrderLineMap : ClassMap<OrderLine>
{
public OrderLineMap()
{
Table("ORDER_LINE");
Id(x => x.Id, "ORDER_LINE_ID");
//USER_ID is a GUID. Column default in DB is Guid.Empty
References(x => x.User, "USER_ID").Cascade.SaveUpdate();
}
}
我有一个 OrderLine,它引用了 User,但该引用在我们创建订单时不一定存在。它可以在稍后的日期和时间更新。由于此处存在外键关系,因此有一个默认列 GUID.Empty,可以在不指定此列的情况下处理插入。用户表中还有一个“空”用户。
目前,我在数据库中找到这个“空”用户,并将我的 OrderLine 对象的用户引用设置为该实例。这种方法对我来说似乎有点笨拙,我一直在寻找更好的方法。我的数据库中有几个这样的关系。
Is there any way that you can specify in Fluent NHibernate to leave out a column whenever it inserts a record if the objects property is null or some other specific value? Coincidentally the column I want to leave out is a Reference in my mapping. Here is my scenario:
public class OrderLineMap : ClassMap<OrderLine>
{
public OrderLineMap()
{
Table("ORDER_LINE");
Id(x => x.Id, "ORDER_LINE_ID");
//USER_ID is a GUID. Column default in DB is Guid.Empty
References(x => x.User, "USER_ID").Cascade.SaveUpdate();
}
}
I have an OrderLine that has a reference to a User but this reference is not necessarily present at the time we create the order. It can be updated at a later date and time. Since there is a foreign key relationship here there is a column default of GUID.Empty that would handle an insert without specifying this column. There is also an "Empty" user in the Users table.
Currently I am finding this "Empty" user in the database and setting my OrderLine object's User reference to this instance. This way seems kind of clumsy to me and I was looking for a better way to do it. I have several relationships like this in my db.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 ORDER_LINE 表的 USER_ID 列不允许为空,则您可能会坚持当前的设置。如果它确实允许空值,那么您应该将 User 属性的类型设置为 Guid?并确保在保存 OrderLine 类之前将其设置为 null。这样,列值在数据库中设置为 null,并且不需要用户表中的“空”行来强制引用完整性。
If the USER_ID column of the ORDER_LINE table does not allow nulls, you're probably stuck with your current setup. If it does allow nulls then you should set the type of the User property to be Guid? and make sure it is set to null before you save the OrderLine class. That way the column value is set to null in the database and it won't require an "Empty" row in the Users table to enforce the referential integrity.