Delete a list of items in Entity Framework

asp.net c# entity-framework entity-framework-6

Question

I am trying to clear all items from an Entity Framework collection, in this case, remove all food from a plate. Any ideas how I can resolve the following - The code below generates the error:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.

Plate selPlate = (Plate)Session["selPlate"];

foreach (FoodForPlate f in selPlate.FoodForPlates)
{
    context.Entry(f).State = System.Data.Entity.EntityState.Deleted;
}

context.SaveChanges();

bindstats();

UpdatePanel1.Update();
1
4
3/14/2014 4:08:10 PM

Popular Answer

Entity Framework keeps track of your loaded items so it can detect changes and you have some of those objects loaded already. Try:

dbContext.Entry(entity).State = EntityState.Detached;

You can also try disabling object tracking but that depends on your application requirements.

1
3/14/2014 5:33:20 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow