In the article how to change the state of an entity using ChangeObjectState method of ObjectStateManager, we have explained why should we change the state of an entity before calling SaveChanges() method in a disconnected scenario.
In a connected scenario, changing the state of an entity before calling SaveChanges() method is not necessary because the context we use to retrieve the entity and saving an entity after modification is same. So, the state manager keeps track of both original and current values of all the entities which are bound to the context. So, just calling SaveChanges() is enough to persist the changes.
If an application is in a disconnected environment and in this scenario, whenever we attach an entity using Attach() method to the ObjectContext, it will be added in Unchanged state. Even though we have modified some scalar properties of an entity, ObjectContext cannot recognize this change. So, we need to change the state of an entity before persist an entity to the database. There are many methods available to change the state of an entity. They are:
- ApplyCurrentValues and ApplyOriginalValues
- ChangeState and ChangeObjectState
The above methods are exposed by both ObjectContext and ObjectSet<T> classes but with some exceptions. Note that The ObjectSet<T> methods internally call ObjectContext methods. But, Microsoft recommends to use ObjectSet<T> methods if a corresponding method is available.
In this article, we will see how to update an entity using ApplyCurrentValues() method. To do that, first we need to retrieve the entity (we wish to modify) from the database and call the ApplyCurrentValues method. This method takes the entity as an input and retrieves the same entity in the ObjectContext by using the key and overwrites the current values of the entity by checking property after property (which is in the ObjectContext ) with the entity values (modified entity values) supplied in the ApplyCurrentValues method.
If original values and current values are different, it marks the state of the entity as Modified but the original values (from the database) of the entity will be intact. So, this method updates the current values of one entity with another entity. After this, we need to call SaveChanges() method. For the purpose of demonstration I have created an example for disconnected scenario. In the example, when we call SaveChanges() method, the UPDATE command is generated (optimized UPDATE sql command constructed with modified properties and not with all the scalar properties) because entity is in the Modified state and changes are persisted to the database.
In the above code, after we call ApplyCurrentValues method, we display the current values of the ObjectStateEntry as shown in Fig1: The Figure: shows the original value and current value. The Savechanges method persists the modifications done to the customer entity. Note that by default, the SaveChanges method calls the AcceptAllChanges method after it has done changes to the database. The AcceptAllChanges method updates the original values with the current values and changes entity state to Unchanged. So that synchronization can happen between ObjectStateManager entries and the database.
If we again run the code, we will get the output as shown in Fig 2: both the original value and current value of an entry is same because we have not changed any property of the entity and no persistence happens.
Drawbacks of the ApplyCurrentValues method
As this method overwrites the current values of the ObjectStateEntry, if any of the properties in the input entity is not set, data loss occurs because the values in the input entity is going to be persisted to the database. Another disadvantage is that this method requires an extra round trip to the database to read the data from the database as compared to ChangeObjectstate method.