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

时间:2018-07-12 11:03:51

标签: asp.net entity-framework linq entity-framework-6

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

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

方法1:

db.Item2.RemoveRange(db.Item2.Where(x => x.TemplateID == idT));
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"}

方法2:

var itemD = new Item2() { TemplateID = idT };
using (TestEntities db = new TestEntities())
        {
           db.Item2.Remove(itemD);
           db.SaveChanges();
        }

出现以下错误:

{"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"}

方法3:

替换

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

IList<Item2> itemD = new List<Item2>() {

            new Item2() { TemplateID = idT}

        };

并使用removerange删除

得到错误:

{"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"}

方法4:

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?)

简单删除会出现以下错误:

{"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"}

我不知道还要尝试什么,到底是什么问题。删除单个条目效果很好。

谢谢大家

1 个答案:

答案 0 :(得分:2)

尝试使用Z.EntityFramework.Plus.EF6库:

db.Item2.Where(x => x.TemplateID == idT).Delete();