ChangeTracker.Clear Method

Definition

Stops tracking all currently tracked entities.

public virtual void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Overridable Sub Clear ()

Remarks

DbContext is designed to have a short lifetime where a new instance is created for each unit-of-work. This manner means all tracked entities are discarded when the context is disposed at the end of each unit-of-work. However, clearing all tracked entities using this method may be useful in situations where creating a new context instance is not practical.

This method should always be preferred over detaching every tracked entity. Detaching entities is a slow process that may have side effects. This method is much more efficient at clearing all tracked entities from the context.

Note that this method does not generate StateChanged events since entities are not individually detached.

See EF Core change tracking for more information and examples.

Applies to