nhibernate 映射:带有cascade=“all-delete-orphan”的集合不再被引用

发布于 2024-08-18 23:24:06 字数 2052 浏览 3 评论 0原文

我的流畅映射遇到一些问题。我有一个实体,其中包含实体的子集合,例如 Event 和 EventItems。

如果我将集合的级联映射设置为 AllDeleteOrphan,则在将新实体保存到数据库时会收到以下错误: NHibernate.HibernateException :拥有级联=“all-delete-orphan”的集合不再被拥有的实体实例引用:Core.Event.EventItems

如果我将级联设置为“全部”,它可以正常工作吗?下面是我的类和映射文件:

 public class EventMap : ClassMap<Event>
{
    public EventMap()
    {
        Id(x => x.Id, "Id")
            .UnsavedValue("00000000-0000-0000-0000-000000000000")
            .GeneratedBy.GuidComb();

        Map(x => x.Name);
        HasMany(x => x.EventItems)
            .Inverse()
            .KeyColumn("EventId")
            .AsBag()
            .Cascade.AllDeleteOrphan();
    }
}

  public class EventItemMap : SubclassMap<EventItem>
{
    public EventItemMap()
    {
         Id(x => x.Id, "Id")
            .UnsavedValue("00000000-0000-0000-0000-000000000000")
            .GeneratedBy.GuidComb();
        References(x => x.Event, "EventId");
    }
}



public class Event : EntityBase
{
    private IList<EventItem> _EventItems;

    protected Event()
    {
        InitMembers();
    }

    public Event(string name)
        : this()
    {
        Name = name;
    }

    private void InitMembers()
    {
        _EventItems = new List<EventItem>();
    }

    public virtual EventItem CreateEventItem(string name)
    {
        EventItem eventItem = new EventItem(this, name);
        _EventItems.Add(eventItem);
        return eventItem;
    }

    public virtual string Name { get; private set; }
    public virtual IList<EventItem> EventItems
    {
        get
        {
            return _EventItems.ToList<EventItem>().AsReadOnly();
        }
        protected set
        {
            _EventItems = value;
        }
    }
}

    public class EventItem : EntityBase
{
    protected EventItem()
    {
    }

    public EventItem(Event @event, string name):base(name)
    {
        Event = @event;
    }

    public virtual Event Event { get; private set; }
}

这里很困惑。任何提示都非常感激。

雪佛兰

I am having some probs with my fluent mappings. I have an entity with a child collection of entities i.e Event and EventItems for example.

If I set my cascade mapping of the collection to AllDeleteOrphan I get the following error when saving a new entity to the DB:
NHibernate.HibernateException : A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: Core.Event.EventItems

If I set the cascade to All it works fine? Below are my classes and mapping files:

 public class EventMap : ClassMap<Event>
{
    public EventMap()
    {
        Id(x => x.Id, "Id")
            .UnsavedValue("00000000-0000-0000-0000-000000000000")
            .GeneratedBy.GuidComb();

        Map(x => x.Name);
        HasMany(x => x.EventItems)
            .Inverse()
            .KeyColumn("EventId")
            .AsBag()
            .Cascade.AllDeleteOrphan();
    }
}

  public class EventItemMap : SubclassMap<EventItem>
{
    public EventItemMap()
    {
         Id(x => x.Id, "Id")
            .UnsavedValue("00000000-0000-0000-0000-000000000000")
            .GeneratedBy.GuidComb();
        References(x => x.Event, "EventId");
    }
}



public class Event : EntityBase
{
    private IList<EventItem> _EventItems;

    protected Event()
    {
        InitMembers();
    }

    public Event(string name)
        : this()
    {
        Name = name;
    }

    private void InitMembers()
    {
        _EventItems = new List<EventItem>();
    }

    public virtual EventItem CreateEventItem(string name)
    {
        EventItem eventItem = new EventItem(this, name);
        _EventItems.Add(eventItem);
        return eventItem;
    }

    public virtual string Name { get; private set; }
    public virtual IList<EventItem> EventItems
    {
        get
        {
            return _EventItems.ToList<EventItem>().AsReadOnly();
        }
        protected set
        {
            _EventItems = value;
        }
    }
}

    public class EventItem : EntityBase
{
    protected EventItem()
    {
    }

    public EventItem(Event @event, string name):base(name)
    {
        Event = @event;
    }

    public virtual Event Event { get; private set; }
}

Pretty stumped here. Any tips greatly appreciated.

Chev

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

习惯成性 2024-08-25 23:24:06

您需要使用访问策略来映射 _EventItems,以便 NHibernate 访问私有成员而不是属性。您收到此错误的原因是,当将列表复制到 _EventItems.ToList() 中的新列表时,集合引用发生了更改。试试这个:

public class EventMap : ClassMap<Event>
{
    public EventMap()
    {
        Id(x => x.Id, "Id")
            .UnsavedValue("00000000-0000-0000-0000-000000000000")
            .GeneratedBy.GuidComb();

        Map(x => x.Name);
        HasMany(x => x.EventItems)
            .Access.PascalCaseField(Prefix.Underscore)
            .Inverse()
            .KeyColumn("EventId")
            .AsBag()
            .Cascade.AllDeleteOrphan();
        }
    }
}

You need to map _EventItems using an access strategy so that NHibernate access the private member instead of the property. You're getting this error because the collection reference is changed when the list is copied to a new List in _EventItems.ToList<EventItem>(). Try this:

public class EventMap : ClassMap<Event>
{
    public EventMap()
    {
        Id(x => x.Id, "Id")
            .UnsavedValue("00000000-0000-0000-0000-000000000000")
            .GeneratedBy.GuidComb();

        Map(x => x.Name);
        HasMany(x => x.EventItems)
            .Access.PascalCaseField(Prefix.Underscore)
            .Inverse()
            .KeyColumn("EventId")
            .AsBag()
            .Cascade.AllDeleteOrphan();
        }
    }
}
雾里花 2024-08-25 23:24:06

我认为公认的答案不是一个优雅的方法。这里可能的问题是 Chev 正在从数据库读取事件,然后将新的 EventItem 列表分配给 EventItems 属性。当您忽略先前的子列表并分配新的子列表时,NHibernate 会抛出此异常。

您需要在这里做的是,

如果您想丢弃旧的 EventItems,请执行以下操作:

events.EventItems.Clear();
events.EventItems.Add(new EventItem { blah blah });

I don't think the accepted answer is an elegant approach. The possible problem here is that Chev is reading Events from the database and then assigning a new EventItem list to the EventItems property. NHibernate throws this exception when you just ignore the previous children list and assign a new children list.

What you need to do here is,

If you want to discard the old EventItems, do this instead:

events.EventItems.Clear();
events.EventItems.Add(new EventItem { blah blah });
第几種人 2024-08-25 23:24:06

检查此帖子:NHibernate:从父集合中删除子记录

对已接受答案的评论也有类似的问题。

您可能需要尝试删除 EventItemsAsReadOnly 以检查这是否是原因。

Check this SO post: NHibernate: Delete a child record from the parent collection

The comments to the accepted answer has similar issue.

You may want to try removing AsReadOnly for your EventItems to check if that's the cause.

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