SQL Server Interview Question #283

What does AsNoTracking() do?

ASP.NET Core, EF Core & SQL Server Real-World Scenarios Senior Advanced

Detailed Explanation

AsNoTracking tells EF Core not to track entities returned by a query. It is useful for read-only operations such as reports, catalog pages, API GET endpoints, and dashboards where the entities will not be updated through the same DbContext.

Removing tracking overhead can reduce memory usage and improve query-processing performance. However, it should not be applied blindly when tracked entity behavior is required.

Code Example

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