A.J Alhorr
A.J Alhorr

Reputation: 559

Entity Framework (model-first) deleting multiple entries with same attribute

My question is simple, I have a table in Entity Framework, I want to delete all entries (rows) in that table that have the same attribute TemplateID (foreign key).

I have tried multiple methods, all giving me internal error 500:

Method1:

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

getting the following error:

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

Method2:

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

getting the following error:

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

Method3:

replacing

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

with

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

            new Item2() { TemplateID = idT}

        };

and remove with removerange

getting error:

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

Method 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();

here for some reason DeleteObject gives

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

simple remove gives the following error:

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

I don't know what else to try, and what exactly is the problem. deleting a single entry works perfectly fine.

Thank you all in advance,

Upvotes: 2

Views: 326

Answers (1)

Slava Utesinov
Slava Utesinov

Reputation: 13498

Try to use Z.EntityFramework.Plus.EF6 library:

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

Upvotes: 2

Related Questions