Home > OS >  Entity Framework: AsNoTracking for included child entities
Entity Framework: AsNoTracking for included child entities

Time:10-18

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?

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

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

CodePudding user response:

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

  • Related