AsNoTracking() and Include

Juan Pablo Gomez :

I have a Linq query that fetches an entity and some of its navigation properties.

context.MyEntity
    .AsNoTracking()
    .Include(i=> i.Nav1)
    .Include(i=> i.Nav2)
    .Where(x=> x.Prop1==1)
    .FirstOrDefault();

my question is:

Is the above query enough to not track MyEntity nor the navigation properties NAv1& Nav2 or must I add AsNoTracking for each navigation property?

like this:  

context.MyEntity
    .AsNoTracking()
    .Include(i=> i.Nav1)
    .AsNoTracking()
    .Include(i=> i.Nav2)
    .AsNoTracking()
    .Where(x=> x.Prop1==1)
    .FirstOrDefault();
Daniel :

Use AsNoTracking after you have completed all your query parameters but before you move the data into memory. In this example, you'll want:

context.MyEntity
    .Include(i=> i.Nav1)
    .Include(i=> i.Nav2)
    .Where(x=> x.Prop1==1)
    .AsNoTracking()
    .FirstOrDefault();

Any child objects of the parent entity will not be tracked.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Entity Framework Core: strange behaviour when filtering on Include with AsNoTracking

Where should AsNoTracking be applied?

Mock AsNoTracking Entity Framework

How to combine Find() and AsNoTracking()?

Issue using AsNoTracking with EntityFramework

Entity Framework lazy loading with AsNoTracking()

How to use AsNoTracking in a service layer

AsNoTracking on context properties, query or ChangeTracker?

Updating nested list without AsNoTracking

What difference does .AsNoTracking() make?

Scope of AsNoTracking within LINQ query in Entity Framework

.AsNoTracking() always on for certains DbSets for entity framework

Entity Framework: difference between Detach and AsNoTracking

AsNoTracking method throwing error in linq statement

Does it matter where AsNoTracking in Entity Framework is called

How to query a large DbSet with AsNoTracking and a CancellationToken

Why does AsNoTracking affect DateTime precision?

Does adding AsNoTracking in Entity Framework impact a Count?

The instance of entity type cannot be tracked even with AsNoTracking

include in include

If LINQ query retruns IEnumerable<int> does it matter if I add AsNoTracking()?

AsNoTracking using LINQ Query syntax instead of Method syntax

Can I use EF Core 2.0 'AsNoTracking' in CRUD service GetAll?

Should I always use AsNoTracking in N-Tier MVC App?

Mock or better workaround for AsNoTracking in ASP.NET Core

EfCore REST-API: For stateless application, should query always be AsNoTracking

cannot be tracked because another instance.. AsNoTracking set tho

Lazy-loading is not supported for detached entities or entities that are loaded with 'AsNoTracking'

Why shouldn't we always use AsNoTracking in EF Core?