Entity Framework Core Interview Question #128

How does AsNoTracking improve performance?

Performance, Raw SQL, Stored Procedures & Advanced Queries Senior Advanced

Quick Interview Answer

AsNoTracking prevents query results from being added to the change tracker. It reduces tracking metadata and change-detection overhead for read-only queries. It is useful for list pages, reports, and read-only APIs, but should not be used when the same context must track and save modifications to those entities.

Detailed Explanation

AsNoTracking prevents query results from being added to the change tracker. It reduces tracking metadata and change-detection overhead for read-only queries. It is useful for list pages, reports, and read-only APIs, but should not be used when the same context must track and save modifications to those entities.

Code Example

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