Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top