Export (0) Print
Expand All

Change Tracking and Identity Resolution (Entity Framework)

An ObjectQuery adds new objects returned by the query to the object cache that is maintained by the ObjectStateManager. The Attach method on ObjectContext and Load methods on EntityReference and EntityCollection also add objects to the object cache. There is one ObjectStateManager for each object context. Within an object context, objects can be added, modified, and deleted, independently of the data in the data source. Entity keys are used to perform identity resolution. When multiple queries or load operations in the scope of a single object context return collections of objects that contain duplicates, Object Services only maintains a single instance of an object with a specific entity key in the cache. By default, queries only return objects that are not already in the cache, which means that changes to objects already in the cache are not overwritten. This behavior is controlled by specifying a MergeOption value for queries and load operations. The default value is AppendOnly. This only loads objects that are not already present in the object cache, which means that existing objects are not overwritten. Another way to prevent changes in the object cache from being overwritten by updates from the database is to specify PreserveChanges. This is frequently used to resolve optimistic concurrency exceptions while preserving changes in the local context. For more information, see Saving Changes and Managing Concurrency (Entity Framework). When you specify OverwriteChanges, objects in the cache are replaced by the latest version of objects that have been materialized from the database, even if changes have already been made to these objects. When you execute a query by using a MergeOption value of NoTracking, change tracking and identity resolution are not performed on returned objects. This is because the returned objects are not added to the ObjectStateManager.

See Also

Community Additions

Show:
© 2014 Microsoft