Showing posts with label As No Tracking With Entity Framework Core | RAJESH GAMI. Show all posts
Showing posts with label As No Tracking With Entity Framework Core | RAJESH GAMI. Show all posts

As No Tracking With Entity Framework Core | RAJESH GAMI

Introduction 

Entity Framework's DbContext keeps track of changes made to an entity or object and ensures that the database is updated correctly when the context method SaveChange () is called. When you use an object query to retrieve entities, Entity Framework caches them and keeps track of the changes made to them until the savechanges method is called.

Entity Framework keeps track of query results that return an entity type. You may not want to track some entities because the data is used for display purposes only and no other operations such as inserts, updates, or deletes are performed. For example, display data in a read-only grid.
In the above scenario, untracked queries are useful. Execution is very fast due to the change tracking feature. There are several ways to achieve untracked queries.
 
No-Tracking query using AsNoTracking() extention method
 
The AsNoTracking() extension method returns a new query and the returned entity is not tracked by the context. This means that EF does not perform the additional task of saving the retrieved entity for tracking.
 
Example
  1. using (EntityModelContext context = new EntityModelContext())  
  2. {  
  3.     var employee = context.Employees.AsNoTracking().ToList();    
  4.   
  5.     var employee2 = context.Employees    
  6.                     .Where(p => p.Id >= 4)    
  7.                     .AsNoTracking().ToList();    
  8. }  
Changing tracking behavior at the context instance level
 
You can also change the default tracking behavior at the Context Instance level. The ChangeTracker class (defined as a property of the context class) has a property QueryTrackingBehavior that you can use to change the tracking behavior.
 
Example
  1. using (EntityModelContext context = new EntityModelContext())  
  2. {  
  3.     context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;  
  4.     ...  
  5.     ...  
  6. }  
If the result set does not contain the entity type, no tracking will be performed.
 
If the query result does not contain the entity type, it will not be tracked by the change tracker and no tracking will be performed. No tracking is performed in the following query. This query returns an anonymous type that contains some of the entity values.
  1. var data2 = context.Employees.Select(p => new  
  2.                 {  
  3.                     name = p.Name,  
  4.                     id = p.Id  
  5.   
  6.                 }).ToList();  
Because the result contains the entity type, the result set may not return the entity even if that entity is tracked by default. In the following query, the result is an anonymous type, but it is tracked because the employee instance is included in the result set.
  1. var data2 = context.Employees.Select(emp => new  
  2.                {  
  3.                    Id = emp.Id,  
  4.                 Employee = emp  
  5.   
  6.                }).ToList();  
No Tracking saves both execution time and memory usage. Applying this option is very important when retrieving large amounts of data from the database for read-only purposes.
 

    RAJESH GAMI - Blog

    Digital Signature Pad in Angular | RAJESH GAMI

      What is Signature Pad? Signature Pad could be a JavaScript library for drawing fancy signatures. It supports HTML5 canvas and uses variabl...