nhibernate 映射:带有cascade=“all-delete-orphan”的集合不再被引用
我的流畅映射遇到一些问题。我有一个实体,其中包含实体的子集合,例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用访问策略来映射 _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:我认为公认的答案不是一个优雅的方法。这里可能的问题是 Chev 正在从数据库读取事件,然后将新的
EventItem
列表分配给EventItems
属性。当您忽略先前的子列表并分配新的子列表时,NHibernate 会抛出此异常。您需要在这里做的是,
如果您想丢弃旧的
EventItems
,请执行以下操作: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 theEventItems
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:检查此帖子:NHibernate:从父集合中删除子记录
对已接受答案的评论也有类似的问题。
您可能需要尝试删除
EventItems
的AsReadOnly
以检查这是否是原因。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 yourEventItems
to check if that's the cause.