Delete a list of items in the Entity Framework

I am trying to clear all the elements from the Entity Framework collection, in this case remove all the food from the plate. Any ideas how I can solve the following: the code below raises an error:

An exception of type "System.InvalidOperationException" occurred in EntityFramework.dll, but was not processed in the user code

Additional information: object-object cannot refer to several 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();
+4
source share
1 answer

The Entity Framework tracks your loaded items to detect changes, and you already have some of these objects. Try:

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

, .

+1

Source: https://habr.com/ru/post/1531783/


All Articles