插入时排除列

发布于 2024-10-28 00:32:51 字数 652 浏览 0 评论 0原文

如果对象属性为 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 技术交流群。

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

发布评论

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

评论(1

稳稳的幸福 2024-11-04 00:32:51

如果 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.

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