VKinfotek Inc.
  • Us
    About Us
    Contact Us
  • FAQs
  • Ready Software
    Ready ERP ASP Core Software for Azure Cloud
    Ready Web ERP Software
    Ready ASP.Net Azure Software
    Ready C# SQL Server Accounting Software
    Ready ASP.Net MVC and EF Software
First slide
Earn $100/hour in USA! Click Here

Update an entity in a disconnected scenario using ApplyCurrentValues method

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:

  1. ApplyCurrentValues and ApplyOriginalValues
  2. AcceptAllChanges
  3. ChangeState and ChangeObjectState
  4. AddObject
  5. Attach
  6. DeleteObject
  7. Detach
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.

update an entity using ApplyCurrentValues() method example code

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.

screen shot of current values of the ObjectStateEntry

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.

screen output of both the original value and current value of an entry

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.

More Resources
  • POCO class in Entity Framework
  • POCO vs Entity Objects with comparison table
  • Rules to be followed while creating POCOs - 4 important rules
  • How to create a POCO class, integrate with ObjectContext, executing a query with POCOs
  • How to load related POCO entities - loading patterns and their differences in their usage with examples
  • How to perform Lazy Loading with POCO classes
  • What is Change tracking in POCOs, ways of tracking
  • Snapshot change tracking with DetectChanges() method
  • Change tracking with proxies, How to create proxies?
  • Instantiate POCO classes using ObjectContext.CreateObject method
  • Using the DetectChanges() method to Fix-up Relationship in poco entities
  • Entity Framework EntityKey object
  • Retrieving a single entity with GetObjectByKey method of ObjectContext using EntityKey
  • How to create an EntityKey in Entity Framework
  • Loading Related entities in EF
  • How to change the state of an entity using ChangeObjectState method of ObjectStateManager
  • ChangeState method of ObjectStateEntry class
  • Using ApplyOriginalValues method
  • ObjectStateEntry class
  • Entity state in Entity Framework - what, how, why?
  • AcceptAllChanges and SaveChanges methods in Entity Framework
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net


  • About Us
  • SITEMAP
  • Terms of use
  • Testimonials

Back to top

Copyright © 2016 - All Rights Reserved - VKInfotek.com