简体   繁体   English

从ef核心中的子集合中删除一些项目

[英]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. 如果我使用RemoveRange删除选定的子RemoveRange ,则RemoveRange不会更新。 If I use Remove to remove the children from the child collection then (apparently) it's not as efficient as using RemoveRange . 如果我使用RemoveRemoveRange删除子项,那么(显然)它不如使用RemoveRange那样有效。

So I have to use RemoveRange to delete the children efficiently and then use Remove to remove them from the child collection. 所以我必须使用RemoveRange地删除子项,然后使用RemoveRemoveRange中删除它们。 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 . 使用RemoveRange ,您将节省时间在于从RemoveRange中删除对象的开销,特别是在更改跟踪时,对于每个具有Remove对象,它将运行一次而不是一次。

Either way, you will only make one trip to the database when you call SaveChanges . 无论哪种方式,当您调用SaveChanges时,您只会访问数据库。

So, in your example, RemoveRange is redundant and can be omitted. 因此,在您的示例中, RemoveRange是多余的,可以省略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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