当将新项目插入非空集合时,NHibernate 级联集合删除
我有以下流畅的映射:
public ScanDeliverySessionMap()
{
Id(x => x.Id);
...
...
HasManyToMany(x => x.ToScanForms) <--- IList<Form> ToScanForms --->
.Table("ToScanForm")
.ParentKeyColumn("SessionId")
.ChildKeyColumn("FormId").Cascade.SaveUpdate();
}
public FormMap()
{
Id(x => x.Id).Column("FormID").GeneratedBy.Foreign("Log");
....
....
HasManyToMany(x => x.ScanDeliverySessions)
.Table("ToScanForm")
.ParentKeyColumn("FormId")
.ChildKeyColumn("SessionId").Inverse();
}
当我尝试将新表单插入 ToScanForms 集合时 一切看似正常,但在 NProf 上观看 我看到 NH casacde DELETE 删除了所有 ToScanForms 项目 然后 NH INSERT ToScanForms 项目(包括新项目)。
一些截图:
I have the following Fluent Mappings:
public ScanDeliverySessionMap()
{
Id(x => x.Id);
...
...
HasManyToMany(x => x.ToScanForms) <--- IList<Form> ToScanForms --->
.Table("ToScanForm")
.ParentKeyColumn("SessionId")
.ChildKeyColumn("FormId").Cascade.SaveUpdate();
}
public FormMap()
{
Id(x => x.Id).Column("FormID").GeneratedBy.Foreign("Log");
....
....
HasManyToMany(x => x.ScanDeliverySessions)
.Table("ToScanForm")
.ParentKeyColumn("FormId")
.ChildKeyColumn("SessionId").Inverse();
}
When I try to insert new Form to the ToScanForms collection
Everything seemingly works properly but watching on NHProf
I see that NH casacde DELETE over all ToScanForms items
and then NH INSERT the ToScanForms items including the new item.
Some screenshots:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种行为是因为 nhibernate 不知道集合中哪些实体是新的、哪些是旧的,因此他必须删除所有内容,然后重新插入它们。
要防止这种行为非常简单:将属性更改为 ICollection 并将 HasManyToMany 映射为集合。您的映射将更改为以下内容:
在底层,nhibernate 将使用 Iesi Collections 的 HashSet,因此现在他知道哪些实体是新的,哪些是旧的。
That behaviour occurs because nhibernate doesn't know which entities in the collection are new and which are old, so he must delete everything and then re-insert them.
To prevent this is behaviour is quite simple: change your property to an ICollection and map your HasManyToMany as a set. You mapping would be changed to the following:
Under the hood nhibernate will use Iesi Collections' HashSet, so now he knows which entities are new and which are old.