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.

  • 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.