在实体框架中删除 [英] delete in entity framework

查看:80
本文介绍了在实体框架中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我将此代码用于实体框架中的删除记录(多个记录)

  var 标签= 来自 t  objLib.TagsField 其中 t.Book_ID_FK == id  select  t;
          foreach ( var  t  in 标签中)
         {
             objLib.TagsField.Context.DeleteObject(t);
             objLib.SaveChanges();
         }



我如何在不使用每个代码的情况下将其删除(这需要花费时间!)

解决方案

我没有发现foreach有问题,但是尝试携带SaveChanges跳出循环.如果您确实有与foreach相关的性能泄漏,那么最好反复进行SaveChanges.无需为每个对象重复此操作.

试试:

  foreach ( var  @object 标签中)
   objLib.TagsField.Context.DeleteObject(@object);
objLib.SaveChanges(); 



—SA



EntityFramework没有提供任何需要删除实体列表的功能...当您调用DeleteObject时,它只是标记应在SaveChanges上删除实体...

因此,即使在EF级别中有一个功能,例如

DeleteObjects(IList< object> toBeDeleted)...它必须遍历每个元素以标记为已删除吗?

如果您遇到性能问题,这不是因为您正在使用foreach,而是因为您在side foreach中调用SaveChanges ...我想对SaveChanges的调用必须遍历整个上下文,以检查要考虑的实体是什么更新或删除..这显然需要时间吗?

正如SAKryukov所提到的,最好一次调用SaveChanges.

希望这对您有帮助...


hi i use this code for delete records in entity framework(several record)

var tag = from t in objLib.TagsField where t.Book_ID_FK == id select t;
         foreach (var t in tag)
         {
             objLib.TagsField.Context.DeleteObject(t);
             objLib.SaveChanges();
         }



how i delete it without use for each this code ( it Takes time!!)

解决方案

I see nothing wrong with foreach, but try to carry SaveChanges out of the loop. If you really have a performance leak related to foreach, it''s rather in repeated SaveChanges. There is no need to repeat it for each object.

Try:

foreach (var @object in tag)
   objLib.TagsField.Context.DeleteObject(@object);
objLib.SaveChanges();



—SA


Hi
EntityFramework has not provided any function that takes list of entities to delete...When you call DeleteObject it just marks that entity should be deleted on SaveChanges...

So even if there is a function in EF level like

DeleteObjects(IList<object> toBeDeleted)...it has to loop through each element to mark as deleted right?

If you are facing performance issue, this is not because you are using foreach, it is because you are calling SaveChanges in side foreach... I guess call to SaveChanges has to go through whole context to check what are the entities to be consider for update or delete..obviuosly this takes time right?

As SAKryukov mentioned it is better call SaveChanges in one-go.

Hope this helps you...


这篇关于在实体框架中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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