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

后端 未结 3 599
北恋
北恋 2020-12-16 12:50

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 mappin

相关标签:
3条回答
  • 2020-12-16 13:18

    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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-16 13:21

    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 });
    
    0 讨论(0)
  • 2020-12-16 13:31

    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.

    0 讨论(0)
提交回复
热议问题