Global setting for AsNoTracking?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
AsNoTracking is a powerful feature in Entity Framework (EF) that provides developers with the capability to retrieve data without tracking the changes to entities in the Entity Framework context. This can drastically improve the performance and efficiency of an application when querying data that doesn't need to be updated or managed in the object state manager—such as read-only operations.
Understanding AsNoTracking
In Entity Framework, tracking refers to the ability of the context to keep track of the objects that have been retrieved from the database. This is essential for update, insert, and delete operations, as the context needs to know the current state of the entities to generate the correct SQL commands.
When you use `AsNoTracking()`, you are telling EF that the context does not need to keep track of changes for these entities. This can lead to performance improvements by reducing overhead because the context will not need to populate the change tracker.
Benefits of Using AsNoTracking
- Performance Enhancement: Since EF does not have to store the information regarding the state of entities, it reduces memory consumption and improves query performance. This is particularly beneficial for applications that deal with large datasets and require only read operations.
- Reduced Latency: As there is less processing required for change tracking, data retrieval can be faster. This becomes significantly evident in high-traffic applications where quick data access is crucial.
- Scalability: Applications can scale better as they can handle more users and operations per second when AsNoTracking is used judiciously.
How to Use AsNoTracking
The `AsNoTracking` method can be invoked on the IQueryable returned from an EF LINQ query. Below is an example demonstrating its usage:
- Write Operations: Entities retrieved as non-tracked cannot be directly updated or deleted. Attempting to modify them without reattaching them to the context will result in an error.
- Complex Queries: When using `Include()`, you might face challenges tracking the entity relationships when necessary, as `AsNoTracking` does not cover navigation properties.
- Mixed Queries: In scenarios where a mixture of tracked and non-tracked queries are required, one must ensure the default tracking behavior suits the majority usage pattern or manage exceptions programmatically.
Related reading
- GO statements blowing up sql execution in .NET
- Good approach to switch database for Test mode / Sand box mode Rails 6
- Good books/articles about spatial indexes
- good noSQL? database for physical measurements
- Go multiple len calls vs performance?
- Golang service running on Kubernetes EKS gets OOM killed high RES memory value, low runtime.Memstats.Alloc value
- Good GetHashCode override for List of Foo objects respecting the order
- Good introduction to the .NET Reactive Framework

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.