Ask a questionAsk a question
 

AnswerUndo entity changes, DLINQ

  • Thursday, August 09, 2007 2:25 PMRick, Rytis Ilciukas Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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  Sad

Answers

All Replies

  • Thursday, August 09, 2007 7:13 PMXun Sun - MSFTMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    try this:

    DataContext.Refresh(RefreshMode.OverwriteCurrentValues, IEnumerable entities)

  • Friday, August 10, 2007 4:27 PMDavid Buchanan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    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;

    }

  • Saturday, August 11, 2007 5:51 PMRick, Rytis Ilciukas Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.

  • Saturday, August 11, 2007 6:33 PMAnders Borum Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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 Snippet

    dbDataContext 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.

  • Monday, August 13, 2007 11:42 AMCtrl-Alt-Del Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?

  • Monday, August 13, 2007 12:04 PMCtrl-Alt-Del Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Tuesday, November 03, 2009 6:23 AMCuzzlor Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I used this guys RevertChanges DataContext extension method. Perfect!

    http://xacc.wordpress.com/2009/02/17/datacontextrevertchanges/