我正在使用实体框架从表中删除几个项目。没有外键/父对象,所以我不能用OnDeleteCascade处理这个。

现在我正在做这个:

var widgets = context.Widgets
    .Where(w => w.WidgetId == widgetId);

foreach (Widget widget in widgets)
{
    context.Widgets.DeleteObject(widget);
}
context.SaveChanges();

它工作,但foreach困扰我。我使用EF4,但我不想执行SQL。我只是想确保我没有错过任何东西-这是最好的,对吧?我可以用扩展方法或helper来抽象它,但在某些地方我们还是要用foreach,对吧?


当前回答

最后,实体框架核心7通过ExecuteDelete命令引入了这个功能:

context.Widgets
           .Where(w => w.WidgetId == widgetId)
           .ExecuteDelete();

这里需要注意的是,ExecuteDelete不需要SaveChanges,根据它的文档:

该操作立即对数据库执行,而不是延迟到DbContext.SaveChanges()被调用。它也不以任何方式与EF更改跟踪器交互:当调用此操作时恰好被跟踪的实体实例不会被考虑,也不会被更新以反映更改。

我知道问题是EF4,但如果升级可以是一个选择!

其他回答

从服务器上拉回任何东西来删除它看起来仍然很疯狂,但至少只返回id比拉下完整的实体要精简得多:

var ids = from w in context.Widgets where w.WidgetId == widgetId select w.Id;
context.Widgets.RemoveRange(from id in ids.AsEnumerable() select new Widget { Id = id });

你可以使用像EntityFramework这样的扩展库。Extended或Z.EntityFramework.Plus。EF6,有EF 5, 6或核心可用。当你必须删除或更新时,这些库具有出色的性能,并且它们使用LINQ。删除(source +)的示例:

ctx.Users。Where(x => x. lastlogindate < DateTime.Now.AddYears(-2)) delete ();

或(来源扩展)

context.Users。Where(u => u.FirstName == "firstname") delete ();

它们使用本地SQL语句,因此性能非常好。

这已经够好了,对吧?我可以用扩展来抽象它 方法或助手,但我们还是会用到 foreach ?

是的,不过你可以把它写成两行字:

context.Widgets.Where(w => w.WidgetId == widgetId)
               .ToList().ForEach(context.Widgets.DeleteObject);
context.SaveChanges();

实体框架核心

3.1 3.0 2.2 2.1 2.0 1.1 1.0

using (YourContext context = new YourContext ())
{
    var widgets = context.Widgets.Where(w => w.WidgetId == widgetId);
    context.Widgets.RemoveRange(widgets);
    context.SaveChanges();
}

简介:

从集合下面的上下文中移除给定的实体集合 每个实体都处于已删除状态,这样它就会被删除 当SaveChanges被调用时,从数据库中获取。

备注:

Note that if System.Data.Entity.Infrastructure.DbContextConfiguration.AutoDetectChangesEnabled is set to true (which is the default), then DetectChanges will be called once before delete any entities and will not be called again. This means that in some situations RemoveRange may perform significantly better than calling Remove multiple times would do. Note that if any entity exists in the context in the Added state, then this method will cause it to be detached from the context. This is because an Added entity is assumed not to exist in the database such that trying to delete it does not make sense.

EntityFramework 6使用. removerange()使这变得更容易一些。

例子:

db.People.RemoveRange(db.People.Where(x => x.State == "CA"));
db.SaveChanges();

警告!不要在大型数据集上使用此方法!

EF将所有数据拉入内存,然后删除它。对于较小的数据集,这可能不是一个问题,但通常避免这种类型的删除,除非你能保证你只做非常小的更改。

您可以很容易地将进程运行到内存不足的状态,而EF会很高兴地提取您指定的所有数据来删除它。