简体   繁体   中英

deleting some items from a child collection in ef core

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM