Asp.net 实体框架(模型优先)删除具有相同属性的多个条目

Asp.net 实体框架(模型优先)删除具有相同属性的多个条目,asp.net,entity-framework,linq,entity-framework-6,Asp.net,Entity Framework,Linq,Entity Framework 6,我的问题很简单,我在实体框架中有一个表,我想删除该表中具有相同属性TemplateID(外键)的所有条目(行) 我尝试了多种方法,所有方法都给我内部错误500: 方法1: db.Item2.RemoveRange(db.Item2.Where(x => x.TemplateID == idT)); db.SaveChanges(); var itemD = new Item2() { TemplateID = idT }; using (TestEntities db = new Tes

我的问题很简单,我在实体框架中有一个表,我想删除该表中具有相同属性TemplateID(外键)的所有条目(行)

我尝试了多种方法,所有方法都给我内部错误500:

方法1:

db.Item2.RemoveRange(db.Item2.Where(x => x.TemplateID == idT));
db.SaveChanges();
var itemD = new Item2() { TemplateID = idT };
using (TestEntities db = new TestEntities())
        {
           db.Item2.Remove(itemD);
           db.SaveChanges();
        }
var itemtD = db.Item2
                .Where(i => i.TemplateID == idT);

            foreach (Item2 ite in itemtD)
            {
                db.Item2.Remove(ite);
                //db.Item2.DeleteObject(ite);

            }

            db.SaveChanges();
获取以下错误:

{"Message":"Attaching an entity of type \u0027CustomTemplates.Models2.CustomTemplate\u0027 failed because another entity of the same type already has the same primary key value. This can happen when using the \u0027Attach\u0027 method or setting the state of an entity to \u0027Unchanged\u0027 or \u0027Modified\u0027 if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the \u0027Add\u0027 method or the \u0027Added\u0027 entity state to track the graph and then set the state of non-new entities to \u0027Unchanged\u0027 or \u0027Modified\u0027 as appropriate.","StackTrace":"   at System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet\u0026 entitySet, Boolean\u0026 isNoOperation)\r\n   at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.\u003c\u003ec__DisplayClassa.\u003cAttach\u003eb__9()\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)\r\n   at System.Data.Entity.Internal.InternalEntityEntry.set_State(EntityState value)\r\n   at System.Data.Entity.Infrastructure.DbEntityEntry`1.set_State(EntityState value)\r\n   at CustomTemplates.WebService1.DeleteTemplate(String itemid) in C:\\Users\\A.J\\source\\repos\\CustomTemplates\\WebService1.asmx.cs:line 243","ExceptionType":"System.InvalidOperationException"}
{"Message":"The object cannot be deleted because it was not found in the ObjectStateManager.","StackTrace":"   at System.Data.Entity.Core.Objects.ObjectContext.DeleteObject(Object entity, EntitySet expectedEntitySet)\r\n   at System.Data.Entity.Core.Objects.ObjectContext.DeleteObject(Object entity)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.RemoveRange(IEnumerable entities)\r\n   at System.Data.Entity.DbSet`1.RemoveRange(IEnumerable`1 entities)\r\n   at CustomTemplates.WebService1.DeleteTemplate(String itemid) in C:\\Users\\A.J\\source\\repos\\CustomTemplates\\WebService1.asmx.cs:line 242","ExceptionType":"System.InvalidOperationException"}
{"Message":"The object cannot be deleted because it was not found in the ObjectStateManager.","StackTrace":"   at System.Data.Entity.Core.Objects.ObjectContext.DeleteObject(Object entity, EntitySet expectedEntitySet)\r\n   at System.Data.Entity.Core.Objects.ObjectContext.DeleteObject(Object entity)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.Remove(Object entity)\r\n   at System.Data.Entity.DbSet`1.Remove(TEntity entity)\r\n   at CustomTemplates.WebService1.DeleteTemplate(String itemid) in C:\\Users\\A.J\\source\\repos\\CustomTemplates\\WebService1.asmx.cs:line 242","ExceptionType":"System.InvalidOperationException"}
{"Message":"Attaching an entity of type \u0027CustomTemplates.Models2.CustomTemplate\u0027 failed because another entity of the same type already has the same primary key value. This can happen when using the \u0027Attach\u0027 method or setting the state of an entity to \u0027Unchanged\u0027 or \u0027Modified\u0027 if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the \u0027Add\u0027 method or the \u0027Added\u0027 entity state to track the graph and then set the state of non-new entities to \u0027Unchanged\u0027 or \u0027Modified\u0027 as appropriate.","StackTrace":"   at System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet\u0026 entitySet, Boolean\u0026 isNoOperation)\r\n   at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.\u003c\u003ec__DisplayClassa.\u003cAttach\u003eb__9()\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)\r\n   at System.Data.Entity.Internal.InternalEntityEntry.set_State(EntityState value)\r\n   at System.Data.Entity.Infrastructure.DbEntityEntry`1.set_State(EntityState value)\r\n   at CustomTemplates.WebService1.DeleteTemplate(String itemid) in C:\\Users\\A.J\\source\\repos\\CustomTemplates\\WebService1.asmx.cs:line 243","ExceptionType":"System.InvalidOperationException"}
方法2:

db.Item2.RemoveRange(db.Item2.Where(x => x.TemplateID == idT));
db.SaveChanges();
var itemD = new Item2() { TemplateID = idT };
using (TestEntities db = new TestEntities())
        {
           db.Item2.Remove(itemD);
           db.SaveChanges();
        }
var itemtD = db.Item2
                .Where(i => i.TemplateID == idT);

            foreach (Item2 ite in itemtD)
            {
                db.Item2.Remove(ite);
                //db.Item2.DeleteObject(ite);

            }

            db.SaveChanges();
获取以下错误:

{"Message":"Attaching an entity of type \u0027CustomTemplates.Models2.CustomTemplate\u0027 failed because another entity of the same type already has the same primary key value. This can happen when using the \u0027Attach\u0027 method or setting the state of an entity to \u0027Unchanged\u0027 or \u0027Modified\u0027 if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the \u0027Add\u0027 method or the \u0027Added\u0027 entity state to track the graph and then set the state of non-new entities to \u0027Unchanged\u0027 or \u0027Modified\u0027 as appropriate.","StackTrace":"   at System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet\u0026 entitySet, Boolean\u0026 isNoOperation)\r\n   at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.\u003c\u003ec__DisplayClassa.\u003cAttach\u003eb__9()\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)\r\n   at System.Data.Entity.Internal.InternalEntityEntry.set_State(EntityState value)\r\n   at System.Data.Entity.Infrastructure.DbEntityEntry`1.set_State(EntityState value)\r\n   at CustomTemplates.WebService1.DeleteTemplate(String itemid) in C:\\Users\\A.J\\source\\repos\\CustomTemplates\\WebService1.asmx.cs:line 243","ExceptionType":"System.InvalidOperationException"}
{"Message":"The object cannot be deleted because it was not found in the ObjectStateManager.","StackTrace":"   at System.Data.Entity.Core.Objects.ObjectContext.DeleteObject(Object entity, EntitySet expectedEntitySet)\r\n   at System.Data.Entity.Core.Objects.ObjectContext.DeleteObject(Object entity)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.RemoveRange(IEnumerable entities)\r\n   at System.Data.Entity.DbSet`1.RemoveRange(IEnumerable`1 entities)\r\n   at CustomTemplates.WebService1.DeleteTemplate(String itemid) in C:\\Users\\A.J\\source\\repos\\CustomTemplates\\WebService1.asmx.cs:line 242","ExceptionType":"System.InvalidOperationException"}
{"Message":"The object cannot be deleted because it was not found in the ObjectStateManager.","StackTrace":"   at System.Data.Entity.Core.Objects.ObjectContext.DeleteObject(Object entity, EntitySet expectedEntitySet)\r\n   at System.Data.Entity.Core.Objects.ObjectContext.DeleteObject(Object entity)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.Remove(Object entity)\r\n   at System.Data.Entity.DbSet`1.Remove(TEntity entity)\r\n   at CustomTemplates.WebService1.DeleteTemplate(String itemid) in C:\\Users\\A.J\\source\\repos\\CustomTemplates\\WebService1.asmx.cs:line 242","ExceptionType":"System.InvalidOperationException"}
{"Message":"Attaching an entity of type \u0027CustomTemplates.Models2.CustomTemplate\u0027 failed because another entity of the same type already has the same primary key value. This can happen when using the \u0027Attach\u0027 method or setting the state of an entity to \u0027Unchanged\u0027 or \u0027Modified\u0027 if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the \u0027Add\u0027 method or the \u0027Added\u0027 entity state to track the graph and then set the state of non-new entities to \u0027Unchanged\u0027 or \u0027Modified\u0027 as appropriate.","StackTrace":"   at System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet\u0026 entitySet, Boolean\u0026 isNoOperation)\r\n   at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.\u003c\u003ec__DisplayClassa.\u003cAttach\u003eb__9()\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)\r\n   at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)\r\n   at System.Data.Entity.Internal.InternalEntityEntry.set_State(EntityState value)\r\n   at System.Data.Entity.Infrastructure.DbEntityEntry`1.set_State(EntityState value)\r\n   at CustomTemplates.WebService1.DeleteTemplate(String itemid) in C:\\Users\\A.J\\source\\repos\\CustomTemplates\\WebService1.asmx.cs:line 243","ExceptionType":"System.InvalidOperationException"}
方法3:

db.Item2.RemoveRange(db.Item2.Where(x => x.TemplateID == idT));
db.SaveChanges();
var itemD = new Item2() { TemplateID = idT };
using (TestEntities db = new TestEntities())
        {
           db.Item2.Remove(itemD);
           db.SaveChanges();
        }
var itemtD = db.Item2
                .Where(i => i.TemplateID == idT);

            foreach (Item2 ite in itemtD)
            {
                db.Item2.Remove(ite);
                //db.Item2.DeleteObject(ite);

            }

            db.SaveChanges();
替换

var itemD = new Item2() { TemplateID = idT };

方法4:

db.Item2.RemoveRange(db.Item2.Where(x => x.TemplateID == idT));
db.SaveChanges();
var itemD = new Item2() { TemplateID = idT };
using (TestEntities db = new TestEntities())
        {
           db.Item2.Remove(itemD);
           db.SaveChanges();
        }
var itemtD = db.Item2
                .Where(i => i.TemplateID == idT);

            foreach (Item2 ite in itemtD)
            {
                db.Item2.Remove(ite);
                //db.Item2.DeleteObject(ite);

            }

            db.SaveChanges();
由于某种原因,这里DeleteObject给出

Error   CS1061  'DbSet<Item2>' does not contain a definition for 'DeleteObject' and no extension method 'DeleteObject' accepting a first argument of type 'DbSet<Item2>' could be found (are you missing a using directive or an assembly reference?)
我不知道还有什么好尝试的,到底是什么问题。删除一个条目效果很好

提前感谢大家,

尝试使用图书馆:


您确定要删除的Item2实体没有在另一个表上引用吗?我很确定,虽然现在您提出了这个问题,但我首先使用了model,EF创建了这样的上下文:public virtual DbSet Item2{get;set;},可能是这导致了它?它对上下文使用相同的名称。无论如何,@Slava Utesinov的解决方案是有效的!!