Entity Framework Core Interview Question #106

What is AsNoTracking and when should it be used?

Change Tracking, Updates, Transactions & Concurrency Mid-Level Intermediate

Quick Interview Answer

AsNoTracking tells EF Core not to track entities returned by a query. It is well suited to read-only pages, reports, and API responses where the entities will not be modified through the same context.

Detailed Explanation

AsNoTracking tells EF Core not to track entities returned by a query. It is well suited to read-only pages, reports, and API responses where the entities will not be modified through the same context.

It can reduce change-tracker memory and CPU overhead, especially for larger result sets.

Do not use it when the workflow relies on modifying the queried entity and having SaveChanges automatically persist those modifications.

Code Example

var products = await db.Products
    .AsNoTracking()
    .Where(p => p.IsPublished)
    .ToListAsync();