ASP.NET Core Web API Interview Question #111
What is AsNoTracking in EF Core and when should an API use it?
Dependency Injection, Services, EF Core, Async & Data Access Mid-Level Intermediate
Quick Interview Answer
AsNoTracking tells EF Core not to track returned entities for later updates. It is useful for read-only queries because it can reduce tracking overhead.
Detailed Explanation
AsNoTracking tells EF Core not to track returned entities for later updates. It is useful for read-only queries because it can reduce tracking overhead.
Do not use it blindly when the same entities need to be modified and saved through the current DbContext. Query behavior should reflect whether change tracking is actually required.
Code Example
var products = await _db.Products
.AsNoTracking()
.OrderBy(x => x.Name)
.ToListAsync(token);