gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:47:51 34 4
gpt4 key购买 nike

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

如果我将集合的级联映射设置为 AllDeleteOrphan,则在将新实体保存到数据库时会出现以下错误:NHibernate.HibernateException:具有cascade=“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; }
}

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

雪佛兰

最佳答案

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

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映射: A collection with cascade ="all-delete-orphan" was no longer referenced,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2127016/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com