C# Interview Question #173

What is AsNoTracking in EF Core?

EF Core, Data Access & Performance Senior Advanced

Quick Interview Answer

AsNoTracking tells EF Core not to track entities returned by a query.

Detailed Explanation

AsNoTracking tells EF Core not to track entities returned by a query.

It is useful for read-only operations because the application does not need change-tracking information for entities that will not be modified and saved.

For list pages, reports, API GET endpoints, and other read-only queries, AsNoTracking can reduce memory and change-tracking overhead. If an entity will be edited in the same context, normal tracking may be more convenient.

Code Example

var products = await _context.Products
    .AsNoTracking()
    .Where(p => p.IsActive)
    .ToListAsync();