ChangeTracker ChangeTracker Class


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)


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.

public ChangeTracker(DbContext context)
Public Sub New(context As DbContext)


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.

public virtual bool AutoDetectChangesEnabled { get; set; }
Public Overridable Property AutoDetectChangesEnabled As Boolean
System.Boolean System.Boolean

Context Context

Gets the context this change tracker belongs to.

public virtual DbContext Context { get; }
Public Overridable ReadOnly Property Context As DbContext

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.

public virtual QueryTrackingBehavior QueryTrackingBehavior { get; set; }
Public Overridable Property QueryTrackingBehavior As QueryTrackingBehavior


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.

public virtual void AcceptAllChanges()
Public Overridable Sub AcceptAllChanges

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.

public virtual void DetectChanges()
Public Overridable Sub DetectChanges

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.

public virtual IEnumerable<EntityEntry> Entries()
Public Overridable Function Entries As IEnumerable(Of EntityEntry)
System.Collections.Generic.IEnumerable<EntityEntry> System.Collections.Generic.IEnumerable(Of EntityEntry)

An entry for each entity being tracked.

Entries() 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.

public virtual IEnumerable<EntityEntry<TEntity>> Entries<TEntity>()where TEntity : class
Public Overridable Function Entries(Of TEntity As Class) As IEnumerable(Of EntityEntry(Of TEntity))
Type Parameters

The type of entities to get entries for.

System.Collections.Generic.IEnumerable<EntityEntry<TEntity>> System.Collections.Generic.IEnumerable(Of EntityEntry(Of TEntity))

An entry for each entity of the given type that is being tracked.

Equals(Object) Equals(Object)

Inherited from System.Object

Equals(Object, Object) Equals(Object, Object)

Inherited from System.Object

GetHashCode() GetHashCode()

Inherited from System.Object

GetType() 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.

public virtual bool HasChanges()
Public Overridable Function HasChanges As Boolean
System.Boolean System.Boolean

True if there are changes to save, otherwise false.

MemberwiseClone() MemberwiseClone()

Inherited from System.Object

ReferenceEquals(Object, Object) ReferenceEquals(Object, Object)

Inherited from System.Object

ToString() ToString()

Inherited from System.Object

TrackGraph(Object, Action) 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).

public virtual void TrackGraph(object rootEntity, Action<EntityEntryGraphNode> callback)
Public Overridable Sub TrackGraph(rootEntity As Object, callback As Action(Of EntityEntryGraphNode))
System.Object System.Object

The entity to begin traversal from.

System.Action<EntityEntryGraphNode> System.Action(Of EntityEntryGraphNode)

An action to configure the change tracking information for each entity. For the entity to begin being tracked, the State must be set.

Explicit Interface Implementations

IInfrastructure.Instance Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure.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.

IStateManager IInfrastructure<IStateManager>.Instance { get; }
ReadOnly WriteOnly Property Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager>.Instance As IStateManager Implements IInfrastructure(Of IStateManager).Instance