Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 我无法使用实体框架删除或更新多个记录_Asp.net_Entity Framework - Fatal编程技术网

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

Asp.net 我无法使用实体框架删除或更新多个记录,asp.net,entity-framework,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 contai

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

我正在尝试下面的代码

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“System.Data.Entity.DbSet”不包含“DeleteObject”的定义,并且找不到接受“System.Data.Entity.DbSet”类型的第一个参数的扩展方法“DeleteObject”(是否缺少using指令或程序集引用?)

您需要使用
Remove
RemoveRange
(至少使用EF6):

或:


您不能删除EF中的多条记录,因为您必须创建一个自定义方法,该方法将接受对象列表并逐个删除它们。@Amit下面是删除Tallmaris提供的多条记录的解决方案谢谢@Tallmarisy欢迎您。。。您能确认两种解决方案是否都有效吗?我将更新答案:)
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());