Entity Framework Core Interview Question #21

What is AsNoTracking in EF Core?

EF Core Fundamentals & DbContext Junior Beginner

Quick Interview Answer

AsNoTracking tells EF Core not to place query results into the change tracker. It is useful for read-only queries where the returned entities will not be modified and saved through the same context.

Detailed Explanation

AsNoTracking tells EF Core not to place query results into the change tracker. It is useful for read-only queries where the returned entities will not be modified and saved through the same context.

Avoiding tracking can reduce memory use and tracking overhead, especially for larger result sets.

Do not use AsNoTracking automatically for workflows that need tracked entity updates. Choose it based on whether change tracking is required.

Code Example

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