Delen via


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 : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager>
public class ChangeTracker : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager>, Microsoft.EntityFrameworkCore.Infrastructure.IResettableService
public class ChangeTracker : Microsoft.EntityFrameworkCore.Infrastructure.IResettableService
type ChangeTracker = class
    interface IInfrastructure<IStateManager>
type ChangeTracker = class
    interface IInfrastructure<IStateManager>
    interface IResettableService
type ChangeTracker = class
    interface IResettableService
Public Class ChangeTracker
Implements IInfrastructure(Of IStateManager)
Public Class ChangeTracker
Implements IInfrastructure(Of IStateManager), IResettableService
Public Class ChangeTracker
Implements IResettableService
Inheritance
ChangeTracker
Implements
IInfrastructure<Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager> IResettableService

Remarks

See EF Core change tracking for more information and examples.

Constructors

ChangeTracker(DbContext, IStateManager, IChangeDetector, IModel, IEntityEntryGraphIterator)

This is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release. You should only use it directly in your code with extreme caution and knowing that doing so can result in application failures when updating to a new Entity Framework Core release.

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.

ChangeTracker(IStateManager, IChangeDetector, IEntityEntryGraphIterator, 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

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

CascadeDeleteTiming

Gets or sets a value indicating when a dependent/child entity will have its state set to Deleted once its parent/principal entity has been marked as Deleted. The default value isImmediate.

Context

Gets the context this change tracker belongs to.

DebugView

Expand this property in the debugger for a human-readable view of the entities being tracked.

Warning: Do not rely on the format of the debug strings. They are designed for debugging only and may change arbitrarily between releases.

See EF Core change tracking for more information and examples.

DeleteOrphansTiming

Gets or sets a value indicating when a dependent/child entity will have its state set to Deleted once severed from a parent/principal entity through either a navigation or foreign key property being set to null. The default value is Immediate.

LazyLoadingEnabled

Gets or sets a value indicating whether navigation properties for tracked entities will be loaded on first access.

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

Methods

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.

CascadeChanges()

Forces immediate cascading deletion of child/dependent entities when they are either severed from a required parent/principal entity, or the required parent/principal entity is itself deleted. See DeleteBehavior.

Clear()

Stops tracking all currently tracked entities.

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

Returns 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>()

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.

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.

TrackGraph(Object, Action<EntityEntryGraphNode>)

Begins tracking an entity and any entities that are reachable by traversing its 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.

TrackGraph<TState>(Object, TState, Func<EntityEntryGraphNode,TState,Boolean>)

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.

Typically traversal of the graph should stop whenever an already tracked entity is encountered or when an entity is reached that should not be tracked. For this typical behavior, use the TrackGraph(Object, Action<EntityEntryGraphNode>) overload. This overload, on the other hand, allows the callback to decide when traversal will end, but the onus is then on the caller to ensure that traversal will not enter an infinite loop.

TrackGraph<TState>(Object, TState, Func<EntityEntryGraphNode<TState>,Boolean>)

Begins tracking an entity and any entities that are reachable by traversing its 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.

Events

DetectedAllChanges

An event fired when any changes have been detected to the entity graph, either through an explicit call to DetectChanges(), or automatically, such as part of executing SaveChanges() or SaveChangesAsync(CancellationToken).

DetectedEntityChanges

An event fired when any changes have been detected to a single entity, either through an explicit call to DetectChanges() or DetectChanges(), or automatically, such as part of executing SaveChanges() or SaveChangesAsync(CancellationToken).

DetectingAllChanges

An event fired when detecting changes to the entity graph about to happen, either through an explicit call to DetectChanges(), or automatically, such as part of executing SaveChanges() or SaveChangesAsync(CancellationToken).

DetectingEntityChanges

An event fired when detecting changes to a single entity is about to happen, either through an explicit call to DetectChanges() or DetectChanges(), or automatically, such as part of executing SaveChanges() or SaveChangesAsync(CancellationToken).

StateChanged

An event fired when an entity that is tracked by the associated DbContext has moved from one EntityState to another.

StateChanging

An event fired when an entity that is tracked by the associated DbContext is moving from one EntityState to another.

Tracked

An event fired when an entity is tracked by the context, either because it was returned from a tracking query, or because it was attached or added to the context.

Tracking

An event fired when an entity is about to be tracked by the context, either because it is returned from a tracking query, or because it is being attached or added to the context.

Explicit Interface Implementations

IInfrastructure<IStateManager>.Instance
Obsolete.

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.

IResettableService.ResetState()

Resets the service so that it can be used from the pool.

IResettableService.ResetStateAsync(CancellationToken)

Extension Methods

ToDebugString(ChangeTracker, ChangeTrackerDebugStringOptions, Int32)

Creates a human-readable representation of the given metadata.

Warning: Do not rely on the format of the returned string. It is designed for debugging only and may change arbitrarily between releases.

GetInfrastructure<T>(IInfrastructure<T>)

Gets the value from a property that is being hidden using IInfrastructure<T>.

This method is typically used by database providers (and other extensions). It is generally not used in application code.

Applies to