nhibernate 映射:一个带有级联 =“all-delete-orphan"的集合不再被引用 [英] nhibernate mapping: A collection with cascade="all-delete-orphan" was no longer referenced

查看:16
本文介绍了nhibernate 映射:一个带有级联 =“all-delete-orphan"的集合不再被引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的流畅映射有一些问题.我有一个带有子实体集合的实体,例如 Event 和 EventItems.

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.

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

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

如果我将级联设置为 All 它工作正常吗?以下是我的类和映射文件:

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.

雪佛

推荐答案

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

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();
        }
    }
}

这篇关于nhibernate 映射:一个带有级联 =“all-delete-orphan"的集合不再被引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆