简体   繁体   English

EF6.1如何在处理大量对象时从父实体快速删除子实体

[英]EF6.1 how to quickly remove child entities from parent entity when dealing with a large number of objects

I've tried searching for this but I fear my criteria isn't correct, I've used examples given but I've still ended up at this point. 我尝试搜索此内容,但我担心我的标准不正确,我使用了给出的示例,但到现在为止仍然结束。

I'm experimenting with EF6.1 and I have a data set consisting of a parent that is linked to a child. 我正在尝试EF6.1,并且有一个数据集,该数据集包含一个链接到子级的父级。 its zero or one to many, and maybe that's my problem. 零或一对多,也许这就是我的问题。

so parent has a table called childID, and a relationship called child. 因此parent具有一个名为childID的表,以及一个名为child的关系。

when I set childID on the parent, EF automatically gets the child object as parent.child and its all good. 当我在父对象上设置childID时,EF会自动将子对象作为parent.child及其所有对象。 if I change the childID, then its mapped to the new child and its all good. 如果我更改childID,则将其映射到新的子项,并且一切正常。

I can bulk update say, 10,000 parents with a new childID and its all fast and good. 我可以批量更新说,有10,000个具有新childID的父母,而且一切都很好。

the issue is this, I need to be able to remove the childID so its back to NULL, thus removing the link to the child from the parent. 问题是,我需要能够删除childID,使其返回NULL,从而从父级中删除到子级的链接。 the child still exists. 这个孩子仍然存在。 tens of thousands of parents may link to this child. 数以万计的父母可能链接到这个孩子。

so far searching here looks like I in fact have to get child.parents.remove(parent); 到目前为止,在这里搜索似乎实际上我必须要获得child.parents.remove(parent);。 and this is where i'm hitting a severe performance issue. 这就是我遇到严重性能问题的地方。 EF seems to lookup every parent that the child has, giving the database some 10,000 record hit, then it removes the correct one. EF似乎在查找孩子拥有的每个父级,从而给数据库打了10,000条记录,然后删除了正确的记录。 but if I want to remove say 9,000 or all 10,000, then since I can only remove one at a time it does 10,000 loops of a 10,000, 9,999, 9,998... sql select, just to then removes one item at a time on each loop, this is extremely slow and the test application just sits there eating cpu time. 但是,如果我想删除9,000个或全部10,000个,则由于我一次只能删除一个,因此它会执行10,000、9,999、9,998 ... sql select的10,000个循环,然后才一次删除每个项循环,这是非常慢的,测试应用程序只是坐在那里吃cpu时间。 an operation such as this takes around 10 minutes. 这样的操作大约需要10分钟。

I had originally thought I could just set parent.childID = null but that does nothing. 我原本以为我可以只设置parent.childID = null,但是却什么也没做。

am I doing something wrong? 难道我做错了什么? is EF just not up to the task? EF不能完成任务吗? is another ORM up to the task? 是另一个ORM完成任务吗? maybe this isn't a job for an ORM anyway? 也许这不是ORM的工作吗?

first post here so please go easy on me :) 首先在这里发布,所以请对我轻松一点:)

I just asked this same question not long ago. 我不久前也问过同样的问题 While EF is nice, sometimes it's not possible to use it when dealing with a huge amount of data. 尽管EF很不错,但有时在处理大量数据时无法使用它。 The solution we came up with involved writing SQL stored procedures and executing those in place of our deletion using EF. 我们提出的解决方案包括编写SQL存储过程,并使用EF执行这些操作来代替我们的删除。

okay so the stored procedure was the way to go, very simple in the end. 好的,所以存储过程就是要走的路,最后非常简单。

create a stored procedure in sql for example 'RemoveChild' and it takes one bigint input parameter called @ParentID 在sql中创建一个存储过程,例如'RemoveChild',它使用一个名为@ParentID的bigint输入参数

which does an UPDATE parents SET ChildID = null WHERE ParentID = @ParentID 哪个执行更新父母SET ChildID = null WHERE ParentID = @ParentID

import that in EF using the model diagram page thing and it created a new public function called dbcontext.RemoveChild(long ParentID). 使用模型图页面内容将其导入EF,并创建了一个名为dbcontext.RemoveChild(long ParentID)的新公共函数。 call that in code and the magic happens instantly. 在代码中调用它,魔术立即发生。 I called it inside a using dbcontext and did a .saveChanges() afterward, not sure if that's needed but that structure was already in place. 我在使用dbcontext的内部调用它,然后执行了.saveChanges(),不确定是否需要,但是该结构已经到位。

note that this particular stored procedure is specific to that entity and not generic, might be a way to do it generically, I haven't looked yet. 请注意,这个特定的存储过程是特定于该实体的而不是通用的,可能是通用的一种方式,我还没有看过。

why EF cant do this I have no idea. 为什么EF不能做到这一点,我不知道。

暂无
暂无

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

相关问题 如何将EF6.1连接字符串从connectionStrings移到appSettings配置文件部分? - How to move EF6.1 connection string from connectionStrings to an appSettings config file section? EF Core - 从父实体删除子实体时出现错误“实体之间的关联已被切断” - EF Core - Error "The association between entities has been severed" while deleting child entity from parent 如何创建由Tuning Advisor(EF6.1代码优先)指定的非聚集索引 - How to create a non clustered index specified by tuning Advisor, EF6.1 Code First 在 Entity Framework Core 中,我的 EF 实体如何从另一个不是实体的表返回值作为查找? - In Entity Framework Core, how can my EF entities return a value from another table that is not an entity as a lookup? 当子对象具有身份密钥时,如何使用EF将多个子实体添加到对象? - How can I use EF to add multiple child entities to an object when the child has an identity key? EF Core 子实体不与父实体一起加载 - The EF Core child entity isn't loaded along with the parent 修改子实体时如何确保父级上的行版本更新 - How to ensure that rowversion on parent updates when a child entity is modified 如何加载具有大量记录的silverlight实体? - How to load silverlight Entities with a large number of records? 如何将子实体更改为父实体? - How to change child entity into parent entity? 实体框架 - 未在保存父实体上添加到数据库的新子实体(仅在生产环境中) - Entity Framework - New child entities not added to database on saving parent entity (only in production environment)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM