我想使用实体框架删除多条记录,但我无法删除。
我正在尝试关注代码
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?)
答案 0 :(得分:2)
您需要使用Remove
或RemoveRange
(至少使用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());