deleting some items from a child collection in ef core

与世无争的帅哥 提交于 2019-12-07 16:13:53

问题


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:


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.



来源:https://stackoverflow.com/questions/42448442/deleting-some-items-from-a-child-collection-in-ef-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!