Reputation: 1089
I follow this tutorial:
foreach (var foo in item.Foos)
{
if (foo.State == PocoState.Deleted)
{
ctx.Entry(foo).State = EntityState.Deleted;
}
}
ctx.SaveChanges();
But I always get an InvalidOperationException because the 'foo' is deleted from item.Foos before calling ctx.SaveChanges();
Upvotes: 0
Views: 44
Reputation: 11990
You can't delete from a list while you are iterating that list. A ToList
will solve the problem because you will iterate a "copy" instead of the list itself.
foreach (var foo in item.Foos.ToList())
{
if (foo.State == PocoState.Deleted)
{
ctx.Entry(foo).State = EntityState.Deleted;
}
}
ctx.SaveChanges();
Upvotes: 3