PreserveChanges

7 posts, 1 answers
  1. Dennis
    Dennis avatar
    12 posts
    Member since:
    Oct 2012

    Posted 09 Nov 2012 Link to this post

    Hi,

    In your example on this page (http://www.telerik.com/help/openaccess-orm/developer-guide-crud-saving-concurrency-conflicts.html), I don't understand the difference between Telerik.OpenAccess.RefreshMode.OverwriteChangesFromStore and Telerik.OpenAccess.RefreshMode.PreserveChanges.

    From what I understand, PreserveChanges should "preserve" the modified values of the object and OverwriteChangesFromStore should "overwrite" the values of the object from the database.  When I try to run your example and an exception occurred, the object is now updated base on the new values read from the database.  I assume calling PreserveChanges would retain the modified values of the object.  In this case, it is not.

    From the documentation: "The PreserveChanges value means that the actual changes in the object will be kept but all clean data will be re-read."


    using ( EntitiesModel dbContext = new EntitiesModel() )
    {
       Category categoryToUpdate = dbContext.Categories.FirstOrDefault();
     
       // Let's assume CategoryName has the original value of "Orig-Name", then we change it to "NewName"
       categoryToUpdate.CategoryName = "NewName";
     
       try
       {
          // Before saving, the DB was modified externally and CategoryName is now "Mod-Name"
           dbContext.SaveChanges();
       }
       catch ( Telerik.OpenAccess.Exceptions.OptimisticVerificationException ex )
       {
            // When exception occurred, the CategoryName is now "Mod-Name"
            //  Calling PreserveChanges does not preserve the value of the object
           dbContext.Refresh(Telerik.OpenAccess.RefreshMode.PreserveChanges, categoryToUpdate);
     
            // In your example, whether PreserveChanges or OverwriteChangesFromStore, you set the name of CategoryName
            //  Why the value of the object is not preserved after Refresh with PreserveChanges mode?
           categoryToUpdate.CategoryName = "NewName";
           dbContext.SaveChanges();
       }
    }

    How do I make sure the changes in the object are kept when an exception occurred?  In this scenario when inside the exception, the object is now updated with the new values from the database.

    - Dennis
  2. Dennis
    Dennis avatar
    12 posts
    Member since:
    Oct 2012

    Posted 13 Nov 2012 Link to this post

    Anybody?

    - Dennis
  3. DevCraft banner
  4. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 14 Nov 2012 Link to this post

    Hi Dennis,

    When the internal transaction during the SaveChanges fails all the changes are being roll backed and updated with the latest changes from the database.

    What you could do is to detach the modified instance before the commit and attach later on when you are able to commit the changes to the database.

    Hope that helps.

    Greetings,
    Damyan Bogoev
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  5. Dennis
    Dennis avatar
    12 posts
    Member since:
    Oct 2012

    Posted 14 Nov 2012 Link to this post

    Hi Damyan,

    Thanks for your answer.

    Base from your answer since the exception would always refresh the object and get the updated values from the database, so calling Refresh with PreserveChanges inside the exception does nothing?

    - Dennis
  6. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 16 Nov 2012 Link to this post

    Hello Dennis,

    I am afraid you are right. 

    You could use the suggested approach in my previous answer in order to handle this case.

    I am sorry for the inconvenience caused on your side.

    Greetings,
    Damyan Bogoev
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  7. Dennis
    Dennis avatar
    12 posts
    Member since:
    Oct 2012

    Posted 16 Nov 2012 Link to this post

    Hi Damyan,

    Thank you for the confirmation.  Maybe you want to update your documentation / example regarding this item.  In your example after an exception occurred you called Refresh, which basically is already called before going into the exception.

    http://www.telerik.com/help/openaccess-orm/developer-guide-crud-saving-concurrency-conflicts.html

    - Dennis
  8. Answer
    Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 21 Nov 2012 Link to this post

    Hello Dannis,

    Thank you for pointing that out. Please find your Telerik Points updated for the feedback you provided for our documentation.

    Greetings,
    Ivailo
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
Back to Top
DevCraft banner