Change tracking with POCOs
In this article, let us discuss the following questions and find answers for them.
What is meant by Change tracking?
Why should we track changes in POCO entities?
Ways of change tracking - Snapshot change tracking and change tracking with proxies
What is meant by change tracking?
When a query is executed, entity objects are returned and ObjectContext holds their references and tracks the changes and maintains state of all entity objects. This feature is known as change tracking or object tracking.
Entity objects residing in the ObjectContext exist in different states. When entities are returned from a query, the entities are in Unchanged state. Any changes we make to an entity object changes its state. All entities will be in one of the following states (see below). In this article, we will not discuss about these entity states in detail but we will discuss change tracking with pocos.
Why should we track changes in POCO entities?
When SaveChanges() method of the ObjectContext is called, the entities which are in the Added state will be added to the table as new rows using INSERT commands and the entities which are in the Modified state will be saved to database using UPDATE commands. Similarly, entities marked as deleted will be deleted from the database.
The SaveChanges method processes all the entities which are attached to the ObjectContext and depending on the entity state of each object, it executes the commands. We should know that depending on the state of an entity, persistence occurs.
The fact is that the Context delegates the change-tracking management to another class that is ObjectStateManger class. The ObjectStateManger class is responsible for:
Maintaining the modifications made to an entity
Keeps track of relationships between entities
Instead of specifying ObjectStateManager, we simply say ObjectContext does all the work. When we add, attach, delete an entity from the context, we actually do all this with the ObjectStateManager. The ObjectContext should be aware of the changes made to entities before calling various SaveChanges commands.
Actually, the ObjectStateManager cannot monitor changes happening to an entity. Entity itself notifies the ObjectStateManager about their changes. The classes that inherit from EntityObject (EF generated classes) interact continuously with the ObjectContext and notify their changes.
But POCO classes cannot communicate back to ObjectContext. So, it is the ObjectContext´s responsibility to synchronize the POCO data with ObjectStateEntry objects. It is necessary to synchronize them before calling SaveChanges() method. If not, the ObjectStateEntry objects will not reflect the changes made to the entities and insert, update and delete commands are not executed against the database and changes to POCO entities will not be persisted back to the database. You may be wondering where are these ObjectStateEntry objects coming from. When we perform a query with POCO, the ObjectContext creates ObjectStateEntry objects as it does with an EntityObject.
Ways of change-tracking mechanisms
They are two ways we can track changes. One is Snapshot Change Tracking using DetectChanges method without using proxies and Change Tracking with Proxies.
What is meant by Change tracking?
Why should we track changes in POCO entities?
Ways of change tracking - Snapshot change tracking and change tracking with proxies
What is meant by change tracking?
When a query is executed, entity objects are returned and ObjectContext holds their references and tracks the changes and maintains state of all entity objects. This feature is known as change tracking or object tracking.
Entity objects residing in the ObjectContext exist in different states. When entities are returned from a query, the entities are in Unchanged state. Any changes we make to an entity object changes its state. All entities will be in one of the following states (see below). In this article, we will not discuss about these entity states in detail but we will discuss change tracking with pocos.
- Added - The entity is set as added
- Deleted - The entity is set as deleted
- Modified - The entity is set as modified
- Unchanged - The entity is set as not been modified
- Detached - The entity is set as not tracked
Why should we track changes in POCO entities?
When SaveChanges() method of the ObjectContext is called, the entities which are in the Added state will be added to the table as new rows using INSERT commands and the entities which are in the Modified state will be saved to database using UPDATE commands. Similarly, entities marked as deleted will be deleted from the database.
The SaveChanges method processes all the entities which are attached to the ObjectContext and depending on the entity state of each object, it executes the commands. We should know that depending on the state of an entity, persistence occurs.
The fact is that the Context delegates the change-tracking management to another class that is ObjectStateManger class. The ObjectStateManger class is responsible for:
Maintaining the modifications made to an entity
Keeps track of relationships between entities
Instead of specifying ObjectStateManager, we simply say ObjectContext does all the work. When we add, attach, delete an entity from the context, we actually do all this with the ObjectStateManager. The ObjectContext should be aware of the changes made to entities before calling various SaveChanges commands.
Actually, the ObjectStateManager cannot monitor changes happening to an entity. Entity itself notifies the ObjectStateManager about their changes. The classes that inherit from EntityObject (EF generated classes) interact continuously with the ObjectContext and notify their changes.
But POCO classes cannot communicate back to ObjectContext. So, it is the ObjectContext´s responsibility to synchronize the POCO data with ObjectStateEntry objects. It is necessary to synchronize them before calling SaveChanges() method. If not, the ObjectStateEntry objects will not reflect the changes made to the entities and insert, update and delete commands are not executed against the database and changes to POCO entities will not be persisted back to the database. You may be wondering where are these ObjectStateEntry objects coming from. When we perform a query with POCO, the ObjectContext creates ObjectStateEntry objects as it does with an EntityObject.
Ways of change-tracking mechanisms
They are two ways we can track changes. One is Snapshot Change Tracking using DetectChanges method without using proxies and Change Tracking with Proxies.
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
- 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
- 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