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.
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.
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.
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.
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
- Update an entity in a disconnected scenario using ApplyCurrentValues 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