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();
GetTable(t).DeleteOnSubmit(item);
}
foreach (var item in cs.Deletes)
{
var t = item.GetType();
GetTable(t).InsertOnSubmit(item);
}
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);
}
}
}
#if DEBUG
cs = GetChangeSet();
Debug.Assert(cs.Deletes.Count == 0);
Debug.Assert(cs.Inserts.Count == 0);
Debug.Assert(cs.Updates.Count == 0);
#endif
}
GREAT method!
Excelent work!
By: Morten on March 28, 2010
at 12:40 pm