public void RejectChanges()
{
foreach (var entry in ChangeTracker.Entries())
{
switch (entry.State)
{
case EntityState.Modified:
case EntityState.Deleted:
entry.State = EntityState.Modified; //Revert changes made to deleted entity.
entry.State = EntityState.Unchanged;
break;
case EntityState.Added:
entry.State = EntityState.Detached;
break;
}
}
}
Update:
Some users suggest to add .ToList()
to avoid ‘collection was modified’ exception.
But I believe there is a reason for this exception.
How do you get this exception? Probably, you are using context in non threadsafe manner.