如何在Entity Framework Core中删除多行? [英] How do I delete multiple rows in Entity Framework Core?

查看:121
本文介绍了如何在Entity Framework Core中删除多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Entity Framework Core从数据库中删除多行。

I need to delete multiple rows from a database using Entity Framework Core.

此代码不起作用:

foreach (var item in items)
{
    myCollection.Remove(item);
}

因为我收到一个错误 InvalidOperationException:集合已修改;枚举操作可能不会执行在第一个对象之后。换句话说, .Remove 仅删除一个对象。

because I get an error "InvalidOperationException: Collection was modified; enumeration operation may not execute" after the first object. In other words, .Remove removes only one object.

Entity Framework Core没有 .RemoveRange ,因此我不知道如何执行此操作。

Entity Framework Core does NOT have .RemoveRange, so I have no idea how to perform this operation.

为了保持与各种数据库提供程序的最大兼容性,我宁愿不调用context.Database.ExecuteSqlCommand(从physical_table中删除...)。有合适的解决方案吗?谢谢!

In order to preserve maximum compatibility with the various database providers, I would prefer NOT to call context.Database.ExecuteSqlCommand("delete from physical_table where..."). Is there a suitable solution? Thanks!

推荐答案


因为我得到一个错误 InvalidOperationException:集合已被修改;枚举操作可能不会执行在第一个对象之后。换句话说,.Remove只删除一个对象。

because I get an error "InvalidOperationException: Collection was modified; enumeration operation may not execute" after the first object. In other words, .Remove removes only one object.

这与EF Core无关,是的, .Remove()仅删除一个对象。但是,您正在尝试修改要迭代的集合。有做到这一点的方法,但这不是

This has nothing to do with EF Core, and, yes, .Remove() only removes one object. However, you are attempting to modify a collection that you are iterating through. There are ways to do this, but this isn't a good route to go.


Entity Framework Core没有.RemoveRange,所以我不知道如何执行此操作。

Entity Framework Core does NOT have .RemoveRange, so I have no idea how to perform this operation.

肯定有至少两种简单的方法可以删除EF Core中的多个记录。而且,EF Core确实具有 RemoveRange()方法-这是 DbSet< TEntity> 上的方法,请参见此处位于API文档(如

There are definitely at least a couple simple ways to delete multiple records in EF Core. And, EF Core does have a RemoveRange() method - it's a method on DbSet<TEntity>, see here in the API docs (as stated in the comment above).

几个选项:


  1. 如果 myCollection 是属于 DbSet< TEntity> 的类型,这样的简单调用就可以解决问题:

  1. If myCollection is of a type that belongs to a DbSet<TEntity>, a simple call such as this will do the trick:

_dbContext.MyEntities.RemoveRange(myCollection);
_dbContext.SaveChanges();


  • 如果 myCollection 实际上是一个导航属性离开您所查询的实体,您可以在集合上调用 .Clear(),而不是迭代并调用 .Remove()

  • If myCollection is actually a navigation property off of an entity that you queried, you can call .Clear() on the collection instead of iterating and calling .Remove().

    var myParentEntity = _dbContext.MyParentEntities
                             .Include(x => x.MyChildrenEntities)
                             .Single(x => x.Id == id);
    myParentEntity.MyChildrenEntities.Clear();
    _dbContext.SaveChanges();
    


  • 如上所述,有很多您的问题缺少上下文-应该发布更完整的代码。我只是在黑暗中刺了几口,以让您使用EF Core正常运行!

    As also commented above, there's a lot of context missing on your question - more complete code should be posted. I'm just taking a couple stabs in the dark to get you up and running with EF Core!

    这篇关于如何在Entity Framework Core中删除多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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