Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add when to use AsNoTracking #23572

Closed
Rick-Anderson opened this issue Oct 20, 2021 · 0 comments · Fixed by #28796
Closed

Add when to use AsNoTracking #23572

Rick-Anderson opened this issue Oct 20, 2021 · 0 comments · Fixed by #28796
Assignees
Labels
doc-enhancement Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

@Rick-Anderson
Copy link
Contributor

Rick-Anderson commented Oct 20, 2021

55 minutes into 10-20-2021 EF standup

have @AndriySvyryd review what I write.

The call stack refers to snapshot change tracking. By default, EF creates a snapshot of every entity's property values when it is first tracked by a DbContext instance. The values stored in this snapshot are then compared against the current values of the entity in order to determine which property values have changed. And when querying EF uses identity resolution, so only one instance is created for a given related entity.

There are two common optimizations for this:

  1. If the entities don't need to be tracked, i.e. they aren't going to be modified and then persisted with SaveChanges, then you can add .AsNoTracking() to the query, however it will no longer use identity resolution and you can get duplicate instances
  2. Otherwise, the entity types can implement INotifyPropertyChanging and INotifyPropertyChanged interfaces to avoid the need for a snapshot when tracking. Or use Trackable Entities to also make it easier to work with disconnected entities.

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants