Reputation: 8980
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();
Upvotes: 4
Views: 1792
Reputation: 240
I've just tried a similar case (please don't critique this - it's not my code!) and you can't place .AsNoTracking() on a .Include() followed by a .ThenInclude() call because it causes a compile-time error:
return _context.Feedback
.Include(x => x.Property).AsNoTracking() // Causes a compile error
.ThenInclude(x => x.Owner).AsNoTracking()
.Include(x => x.Booking)
.ThenInclude(x => x.BookingTravellers).AsNoTracking()
.Include(x => x.Contact).AsNoTracking()
.FirstOrDefault(x => x.Id == id);
Severity Code Description Project File Line Suppression State Error CS1061 'IQueryable' does not contain a definition for 'ThenInclude' and no accessible extension method 'ThenInclude' accepting a first argument of type 'IQueryable' could be found (are you missing a using directive or an assembly reference?)
The compiler is happy with:
return _context.Feedbacks
.Include(x => x.Property)
.ThenInclude(x => x.Owner).AsNoTracking()
.Include(x => x.Booking)
.ThenInclude(x => x.BookingTravellers).AsNoTracking()
.Include(x => x.Contact).AsNoTracking()
.FirstOrDefault(x => x.Id == id);
Upvotes: 0
Reputation: 119017
AsNoTracking
causes the entire query to not be tracked. This includes any child entities that might be returned by the query.
Upvotes: 8