Resetting the original values using ApplyOriginalValues method

In the article updating an entity in a disconnected scenario using ApplyCurrentValues method, we have seen that the ApplyCurrentValues method updates the current values of an entity with the modified entity values. On the other hand, the ApplyOriginalValues method updates the original values of an entity with the values of input entity supplied to this method. The input entity is a detached entity (from the database) as you can see in the code given below.

This method retrieves the same entity in the ObjectContext and overwrites the original values of the modified entity with the input entity. After we call this method and if we retrieve the original and current values of an entity, the output is as shown in Fig1: below.
Now the original values of an entity in the context contain the values of the database and current values of an entity are the values of the modified customer data. The context recognizes the changes between original values and current values and the entity state is changed to Modified and persistence happens.

screen output of the original and current values of an entity after calling ApplyOriginalValue method



screen output of the original and current values of an entity

If you run the code again without changing the customer name, you will see that the current and original values are same as displayed in Fig 2. To test how ApplyOriginalValues works, we will display original and current values. You will see that the values are same and also Attach() method makes the entity state to Unchanged. Now the SaveChanges method will not go through the persistence process.

code snippet of ApplyOriginalValues method