deleting some items from a child collection in ef core

c# entity-framework-core

Question

I have a parent table and child table, where the parent has a one-to-many relationship with the children.

I want to delete some of the children and I want the parent's child collection to reflect that change.

If I delete the selected children using RemoveRange, then the child collection doesn't get updated. If I use Remove to remove the children from the child collection then (apparently) it's not as efficient as using RemoveRange.

So I have to use RemoveRange to delete the children efficiently and then use Remove to remove them from the child collection. Is this correct or is there a better way of doing it?

1
3
7/13/2018 8:16:14 PM

Popular Answer

With RemoveRange, where you will save time is in the overhead around removing the objects from the child collection, especially around change tracking which will run once rather than once for each object with Remove.

Either way, you will only make one trip to the database when you call SaveChanges.

So, in your example, RemoveRange is redundant and can be omitted.

0
7/15/2018 1:46:20 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow