Posted by: leppie | February 17, 2009


Here is a nice extension method I dished up, to revert all changes in the DataContext. 🙂

(drop this in your DataContext partial class, or create a static/extension method for it)

public void RevertChanges()
  var cs = GetChangeSet();

  foreach (var item in cs.Inserts)
    var t = item.GetType();

  foreach (var item in cs.Deletes)
    var t = item.GetType();

  foreach (var item in cs.Updates)
    var t = item.GetType();
    foreach (var mm in GetTable(t).GetModifiedMembers(item))
      var pi = mm.Member as PropertyInfo;
      if (pi != null)
        pi.SetValue(item, mm.OriginalValue, null);

      var fi = mm.Member as FieldInfo;
      if (fi != null)
        fi.SetValue(item, mm.OriginalValue);
  cs = GetChangeSet();
  Debug.Assert(cs.Deletes.Count == 0);
  Debug.Assert(cs.Inserts.Count == 0);
  Debug.Assert(cs.Updates.Count == 0);


  1. GREAT method!
    Excelent work!

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: