No Tracking With Entity Framework Core

DbContext in Entity Framework is responsible for tracking the changes made on the entity or object, so the correct update is done to the database when the SaveChange() method of context is called. When we retrieve entities using an object query, the Entity Framework puts these entities in a cache and tracks whatever changes are made on these entities until the savechanges method is called. Entity Framework tracks the query results that return entity types.
Sometimes we do not want to track some entities because the data is only used for viewing purposes and other operations such as insert, update and delete are not done. For example, the view data in a read-only grid.
In the scenario described above, No-Tracking queries are useful. They are very quick to execute because there is a change tracking setup. There are multiple ways to achieve no-tracking queries.
No-Tracking query using AsNoTracking() extention method
The AsNoTracking() extension method returns a new query and returned entities do not track by the context. It means that EF does not perform any additional task to store the retrieve entities for tracking.
  1. using (EntityModelContext context = new EntityModelContext())  
  2. {  
  3.     var employee = context.Employees.AsNoTracking().ToList();    
  5.     var employee2 = context.Employees    
  6.                     .Where(p => p.Id >= 3)    
  7.                     .AsNoTracking().ToList();    
  8. }  
Change tracking behavior at the context instance level
We can also change the default behavior of tracking at context instance level. The ChangeTracker class (defined as property of context class) has property called QueryTrackingBehavior, using this property we can change behavior of tracking.
  1. using (EntityModelContext context = new EntityModelContext())  
  2. {  
  3.     context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;  
  4.     ...  
  5.     ...  
  6. }  
No tracking is performed when result set does not contain any entity types
If our query result does not contain any entity type, it does not track by the change tracker and no tracking is performed. In the following query, which returns anonymous type with some of the value of entity, tracking does not perform.
  1. var data2 = context.Employees.Select(p => new  
  2.                 {  
  3.                     name = p.Name,  
  4.                     id = p.Id  
  6.                 }).ToList();  
Sometimes, result set does not return an entity, even if this entity tracks by default, due to the fact that the result contains an entity type. In the following query, the result is an anonymous type but instance of employee is in result set, so this will be tracked.
  1. var data2 = context.Employees.Select(emp => new  
  2.                {  
  3.                    Id = emp.Id,  
  4.                 Employee = emp  
  6.                }).ToList();  
No Tracking can save both execution times and memory usage. Applying this option really becomes important when we retrieve a large amount of data from the database for read only purposes.
You can view or download the source code from the following GitHub link


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now