Undo entity changes, DLINQ
Hi folks,
How can i undo changes made to entity without storing and/or copying back original values? I can get unmodified entity using Table.GetOriginalEntityState(entity), but that doesn't quite solve my problem. Table.GetOriginalEntityState() gives me another entity, so now i have two entities - modified and original. Is there any way to put original entity into modified entity's place and remove modified entity from LINQ tracking?
I've posted i about this problem before, but nobody answered me. So i'm trying again. If nobody answers me this time, i'm going to cry
. Seriously

Answers
try this:
DataContext
.Refresh(RefreshMode.OverwriteCurrentValues, IEnumerable entities)
All Replies
try this:
DataContext
.Refresh(RefreshMode.OverwriteCurrentValues, IEnumerable entities)Another way to go: Use the values of the OriginalEntity to reset the values in the ModifiedEntity
public static void LoadAndModifyAndUndo()
{
DataClasses1DataContext MyDC = new DataClasses1DataContext();
System.Data.Linq.ChangeSet MySet;
var query = MyDC.Products.Take(1);
//get my entity
Product MyProduct = query.ToList()[0];
//modify my entity.
MyProduct.ProductName = "foobar";
//Modified:1
MySet = MyDC.GetChangeSet();
Console.WriteLine(MySet.ToString());
//what was the original entity
Product OriginalProduct = MyDC.Products.GetOriginalEntityState(MyProduct);
//reset my entity to the original values
MyProduct.ProductName = OriginalProduct.ProductName;
//Modified:0
MySet = MyDC.GetChangeSet();
Console.WriteLine(MySet.ToString());
return;
}
- Yes, but this involves copying back original values, so i would thave to write Undo method for each entity. Plus more problems would come up when relationship would be introduced. For example if Product would have category list.
MyProduct.Categories.Add(newCategory);
this would be difficult to revert. Hello
With Beta 2 the original entity state contains the original child / parent references (EntitySet / EntityRef). Thus, when you go ahead and make changes (add child entities and so on), requesting the original entity from the DataContext gives you the entity as it was retrieved from the database (or, when it was attached to the DataContext) - including the original references - how about that!.
Consider the following piece of code:
Code SnippetdbDataContext
db = new dbDataContext();db.ObjectTrackingEnabled =
true;db.DeferredLoadingEnabled =
false;Gallery parent = db.Galleries.FirstOrDefault();
parent.Modules.Add(
new Module());// now equals 1
// parent.Modules.Count;
Gallery originalParent = db.Galleries.GetOriginalEntityState(parent);
// now equals 0
// originalParent.Modules.Count;
As you can see, a child is added to the parent node (thus, the .Count is 1). The original entity is then requested from the DataContext and the .Count is inspected to yield a value of 0.
- Let's take that:
parent.Modules.Add(new Module()); // Now I add entity
db.SubmitChanges(); // Changes saved, original values were stored in database
parent.Modules.Add(new Module()); // Now I add another entity
Gallery originalParent = db.Galleries.GetOriginalEntityState(parent); // Trying to get original entity state
Everything here looks fine: fields of parent were restored, but originalParent.Modules.Count now equals to 0, but should be 1. Something is wrong here with this or did I forget something? - And one more question. How parent should be excluded from database and originalParent saved?db.SubmitChanges() saves parent's values to database, but, originalParent stays unsaved. Copying fields is unefficient in my situation.
- I used this guys RevertChanges DataContext extension method. Perfect!
http://xacc.wordpress.com/2009/02/17/datacontextrevertchanges/

