I have a Linq-to-SQL context with changes in an EntitySet. I want to get rid of all the changes I made to it since I last called SubmitChanges()
. I use the following method to revert the state of the context:
public static void RevertChanges(this DataContext context)
{
var changeSet = context.GetChangeSet();
var inserts = changeSet.Inserts.ToList();
var deleteAndUpdate = changeSet.Deletes.Concat(changeSet.Updates).ToList();
foreach (var inserted in inserts)
{
var table = context.GetTable(inserted.GetType());
table.DeleteOnSubmit(inserted);
}
if (deleteAndUpdate.Count > 0)
{
context.Refresh(RefreshMode.OverwriteCurrentValues, deleteAndUpdate);
}
}
This works fine, except that an EntitySet
based on the table doesn't get refreshed, and keeps the changes in it.
What is the best way to reject the modifications in an EntitySet
?