This is a migrated thread and some comments may be shown as answers.

PreserveChanges

6 Answers 110 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dennis
Top achievements
Rank 1
Dennis asked on 10 Nov 2012, 12:45 AM
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

6 Answers, 1 is accepted

Sort by
0
Dennis
Top achievements
Rank 1
answered on 14 Nov 2012, 03:45 AM
Anybody?

- Dennis
0
Damyan Bogoev
Telerik team
answered on 14 Nov 2012, 06:50 PM
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.
0
Dennis
Top achievements
Rank 1
answered on 14 Nov 2012, 11:58 PM
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
0
Damyan Bogoev
Telerik team
answered on 16 Nov 2012, 06:03 PM
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.
0
Dennis
Top achievements
Rank 1
answered on 16 Nov 2012, 11:54 PM
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
0
Accepted
Ivailo
Telerik team
answered on 21 Nov 2012, 01:29 PM
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.
Tags
Data Access Free Edition
Asked by
Dennis
Top achievements
Rank 1
Answers by
Dennis
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Ivailo
Telerik team
Share this question
or