Entity Framework: AsNoTracking for included child entities

Viewed 197

If I use AsNoTracking on the top-level entity, does it get applied to all the child entities?

So for example, if I write like this

context.FirstEntity.AsNoTracking()
    .Include(f => f.ChildEntity_1)
    .ThenInclude(c => c.ChildEntity_1_1)
    .Include(f => f.ChildEntitiy_2);

Will the AsNoTracking be applied to all the child entities automatically since it is applied to the top-level entity?

Or I have to call the function separately for all the child entities too? like this

context.FirstEntity.AsNoTracking()
    .Include(f => f.ChildEntity_1).AsNoTracking()
    .ThenInclude(c => c.ChildEntity_1_1).AsNoTracking()
    .Include(f => f.ChildEntitiy_2).AsNoTracking();
1 Answers

AsNoTracking causes the entire query to not be tracked. This includes any child entities that might be returned by the query.

Related