从ef核心的子集合中删除某些项目 [英] deleting some items from a child collection in ef core

查看:123
本文介绍了从ef核心的子集合中删除某些项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父表和一个子表,其中父表与子表具有一对多关系。

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

我要删除一些子表

如果我使用 RemoveRange 删除选定的孩子,那么子集合不会更新。如果我使用 Remove 从子集合中删除子项,那么(显然)它的效率不如使用 RemoveRange

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.

所以我必须使用 RemoveRange 有效删除子项,然后使用 Remove 从子集合中删除它们。这是正确的方法还是有更好的方法?

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?

推荐答案

使用 RemoveRange ,您可以节省时间的是从子集合中删除对象的开销,特别是在更改跟踪方面,对于每个带有 Remove 。

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.

无论哪种方式,您都只能在调用 SaveChanges 时访问数据库一次。

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

因此,在您的示例中, RemoveRange 是多余的,可以省略。

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

这篇关于从ef核心的子集合中删除某些项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆