Why does Nhibernate request the entire collection to remove a child?

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 the collection, I would write something like:

currentParent.Children.Remove(toBeDeletedChild);

the problem is when the line above wants to be executed, I get numerous database queries.
It seems that it works like a loop that queries all children and searches for such a child. Pseudo:

foreach(child in currentParent.Children)
   if(child==toBeDeletedChild)
      delete(child);

This means that I get 100 database queries to remove a child from a parent with 100 children.

I am using NH3 and Fluent Nhibernate

Parent class mapping:

HasMany(p => p.Children)
                .LazyLoad()
                .Inverse()
                .Cascade.AllDeleteOrphan();
+3
source share
1 answer

public class Parent
 {
  public int Id { get; set; }
  public ICollection<Child> Children { get; set; }
 }

ParentMap

HasMany(p => p.Children).AsSet()
                .LazyLoad()
                .Inverse()
                .Cascade.AllDeleteOrphan();

HashSet .

0

Source: https://habr.com/ru/post/1795437/


All Articles