ChangeTracker ChangeTracker Class

Definition

Provides access to change tracking information and operations for entity instances the context is tracking. Instances of this class are typically obtained from ChangeTracker and it is not designed to be directly constructed in your application code.

public class ChangeTracker : IInfrastructure<IStateManager>
Public Class ChangeTracker
    Implements IInfrastructure(Of IStateManager)
Inheritance
System.ObjectSystem.Object
ChangeTrackerChangeTracker
Implements

Constructors

ChangeTracker(DbContext) ChangeTracker(DbContext)

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

Properties

AutoDetectChangesEnabled AutoDetectChangesEnabled

Gets or sets a value indicating whether the DetectChanges() method is called automatically by methods of DbContext and related classes.

The default value is true. This ensures the context is aware of any changes to tracked entity instances before performing operations such as SaveChanges() or returning change tracking information. If you disable automatic detect changes then you must ensure that DetectChanges() is called when entity instances have been modified. Failure to do so may result in some changes not being persisted during SaveChanges() or out-of-date change tracking information being returned.

Context Context

Gets the context this change tracker belongs to.

QueryTrackingBehavior QueryTrackingBehavior

Gets or sets the tracking behavior for LINQ queries run against the context. Disabling change tracking is useful for read-only scenarios because it avoids the overhead of setting up change tracking for each entity instance. You should not disable change tracking if you want to manipulate entity instances and persist those changes to the database using SaveChanges().

This method sets the default behavior for the context, but you can override this behavior for individual queries using the AsNoTracking<TEntity>(IQueryable<TEntity>) and AsTracking<TEntity>(IQueryable<TEntity>) methods.

The default value is TrackAll. This means the change tracker will keep track of changes for all entities that are returned from a LINQ query.

Methods

AcceptAllChanges() AcceptAllChanges()

Accepts all changes made to entities in the context. It will be assumed that the tracked entities represent the current state of the database. This method is typically called by SaveChanges() after changes have been successfully saved to the database.

DetectChanges() DetectChanges()

Scans the tracked entity instances to detect any changes made to the instance data. DetectChanges() is usually called automatically by the context when up-to-date information is required (before SaveChanges() and when returning change tracking information). You typically only need to call this method if you have disabled AutoDetectChangesEnabled.

Entries() Entries()

Gets an EntityEntry for each entity being tracked by the context. The entries provide access to change tracking information and operations for each entity.

Entries<TEntity>() Entries(Of TEntity)()

Gets an EntityEntry for all entities of a given type being tracked by the context. The entries provide access to change tracking information and operations for each entity.

System.Object.Equals(System.Object) System.Object.Equals(System.Object) Inherited from System.Object
System.Object.Equals(System.Object, System.Object) System.Object.Equals(System.Object, System.Object) Inherited from System.Object
System.Object.GetHashCode() System.Object.GetHashCode() Inherited from System.Object
System.Object.GetType() System.Object.GetType() Inherited from System.Object
HasChanges() HasChanges()

Checks if any new, deleted, or changed entities are being tracked such that these changes will be sent to the database if SaveChanges() or SaveChangesAsync(CancellationToken) is called.

Note that this method calls DetectChanges() unless AutoDetectChangesEnabled has been set to false.

System.Object.MemberwiseClone() System.Object.MemberwiseClone() Inherited from System.Object
System.Object.ReferenceEquals(System.Object, System.Object) System.Object.ReferenceEquals(System.Object, System.Object) Inherited from System.Object
System.Object.ToString() System.Object.ToString() Inherited from System.Object
TrackGraph(Object, Action<EntityEntryGraphNode>) TrackGraph(Object, Action(Of EntityEntryGraphNode))

Begins tracking an entity and any entities that are reachable by traversing it's navigation properties. Traversal is recursive so the navigation properties of any discovered entities will also be scanned. The specified callback is called for each discovered entity and must set the State that each entity should be tracked in. If no state is set, the entity remains untracked.

This method is designed for use in disconnected scenarios where entities are retrieved using one instance of the context and then changes are saved using a different instance of the context. An example of this is a web service where one service call retrieves entities from the database and another service call persists any changes to the entities. Each service call uses a new instance of the context that is disposed when the call is complete.

If an entity is discovered that is already tracked by the context, that entity is not processed (and it's navigation properties are not traversed).

Explicit Interface Implementations

IInfrastructure<IStateManager>.Instance Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager>.Instance

Gets the internal state manager being used to store information about tracked entities.

This property is intended for use by extension methods. It is not intended to be used in application code.

Extension Methods

Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetInfrastructure<Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager>() Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetInfrastructure(Of Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager)()