Tracking Changes in POCO Entities

There are two ways to track changes in POCO ("plain-old" CLR objects) entities: through a change-tracking proxy object or through a snapshot.

Change Tracking with Proxies

When creation of change-tracking proxy objects is enabled for POCO entities, changes that are made to the object graph are tracked automatically by the Entity Framework as they occur. For more information, see Requirements for Creating POCO Proxies and Identity Resolution, State Management, and Change Tracking. If you cannot meet the additional requirements in your POCO classes or if you do not want these proxy objects to be materialized, you can disable proxy class generation. For more information, see Working with POCO Entities

Change Tracking Using a Snapshot

When creation of proxy objects is disabled, changes that are made to the object graph are not tracked automatically by the Entity Framework as they occur. You must instead use the DetectChanges method to report changes.

When an object is attached to the context, a snapshot is taken of its property values. When the DetectChanges method is called, the attached object graph is synchronized with the ObjectStateManager. For information about how to attach objects, see Attaching and Detaching Objects. The DetectChanges method synchronizes the object graph with the state manager by performing the following actions:

  1. Attaches new objects in the graph to the object context. This can occur if you query for a principal object and create an association with a new dependent object.

  2. Updating the state of objects based on comparing current property values to the snapshot of original values (if the snapshot was taken already).

For more information, see How to: Detect Changes in POCO Entities.

Note

If a POCO entity contains a complex type property, changes to members of the instance of the complex type will be detected through the snapshot method even if the entity has a change-tracking proxy. However, if a new instance of the complex type is assigned to a property, the change of the property is tracked in the same manner as other properties.

When the objects and ObjectStateManager are not synchronized, other operations that use the ObjectStateManager, such as adding, attaching, or deleting objects; calling the Refresh method; or even executing queries with a MergeOption value of PreserveChanges can have unpredictable results.

Because of the importance of keeping the ObjectStateManager synchronized with any changes to the values of attached POCO entities, by default the SaveChanges method first calls the DetectChanges method. If you made changes to the graph and plan to use any of the following methods, before calling SaveChanges, we recommend that you call DetectChanges:

See Also

Concepts

Working with POCO Entities