为什么 Nhibernate 查询整个集合以删除子集合?
你好
这些是类:
public class Child
{
public int Id { get; set; }
public int Name { get; set; }
}
public class Parent
{
public int Id { get; set; }
public IList<Child> Children { get; set; }
}
当我想从集合中删除一个子级时,我会写一些类似的内容:
currentParent.Children.Remove(toBeDeletedChild);
问题是当上面的行想要执行时,我会收到大量对数据库的查询。
看起来它是否作为一个循环来查询所有的孩子并寻找 孩子喜欢这样 Pseudo:
foreach(child in currentParent.Children)
if(child==toBeDeletedChild)
delete(child);
这意味着我将向数据库发出 100 个查询,以便从拥有 100 个孩子的父母中删除孩子。
我正在使用 NH3 和 Fluent Nhibernate
父类映射:
HasMany(p => p.Children)
.LazyLoad()
.Inverse()
.Cascade.AllDeleteOrphan();
Hello
These are the classes :
public class Child
{
public int Id { get; set; }
public int Name { get; set; }
}
public class Parent
{
public int Id { get; set; }
public IList<Child> Children { get; set; }
}
when I want to remove a child from collection, I'd write something like :
currentParent.Children.Remove(toBeDeletedChild);
the problem is when the line above wants to be executed, I get numerous queries to database.
It seems if it works as a loop which querys all over the children and looking for
the child like this Pseudo:
foreach(child in currentParent.Children)
if(child==toBeDeletedChild)
delete(child);
It means I'll get 100 queries to database for deleting a child from a parent with 100 children.
I'm using NH3 and Fluent Nhibernate
Mapping of Parent Class:
HasMany(p => p.Children)
.LazyLoad()
.Inverse()
.Cascade.AllDeleteOrphan();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试将 Parent 更改为
ParentMap 中的 和 ParentMap
另外,您还需要使用
HashSet
而不是子集合的列表。I would try to change the Parent to
and in the ParentMap
Also you would need to use
HashSet
instead of list for the children collection.