Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

nhibernate mapping: A collection with cascade="all-delete-orphan" was no longer referenced

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...