我无法使用实体框架删除或更新多个记录

时间:2014-08-18 11:50:40

标签: asp.net entity-framework

我想使用实体框架删除多条记录,但我无法删除。

我正在尝试关注代码

db.FileTypeWithProducts.Where(x => x.ProductID == proid).ToList().ForEach(db.FileTypeWithProducts.DeleteObject);

在上面的陈述vs 2013中给出错误

Error   1   'System.Data.Entity.DbSet<FileTypeWithProduct>' does not contain a definition for 'DeleteObject' and no extension method 'DeleteObject' accepting a first argument of type 'System.Data.Entity.DbSet<FileTypeWithProduct>' could be found (are you missing a using directive or an assembly reference?)

1 个答案:

答案 0 :(得分:2)

您需要使用RemoveRemoveRange(至少使用EF6):

db.FileTypeWithProducts
    .Where(x => x.ProductID == proid).ToList()
    .ForEach(ft => db.FileTypeWithProducts.Remove(ft)); 
    // you need a lambda since the expected method needs to return void.

或:

db.FileTypeWithProducts
    .RemoveRange(db.FileTypeWithProducts.Where(x => x.ProductID == proid).ToList());