views:

23

answers:

1

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?

+2  A: 

The best way?

using (DataContext dc = new DataContext)
{

}

It's the only way to be sure.

A second best option would be to new up an EntitySet and assign it to the property - same way is done in the designer.cs file.

David B
Ugh. This is difficult due to how the DataContext is used. I'm looking for a way to avoid this.
codekaizen
Hey, thanks for the 2nd option - it looks promising. How do I refresh the entities in the EntitySet after I recreate it?
codekaizen
Ok, I solved it by issuing a requery on the context and filling the EntitySet manually.
codekaizen