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

查看:24
本文介绍了如何删除 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: Collection was modified; enumeration operation may not execute".换句话说,.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("delete from physical_table where...").有合适的解决方案吗?谢谢!

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: Collection was modified; enumeration operation may not execute".换句话说,.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 上的一个方法,请参阅 此处在 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 的类型,像这样的简单调用就可以解决问题:

  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天全站免登陆