Entity Framework and AsNoTracking

Introduction

In the Entity Framework based applications, the DbContext / Object Context is responsible for tracking the changes done in the objects, so the correct update is done to the database when the SaveChanges() method of the 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.

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.

The AsNoTracking() extension method returns a new query and the returned entities will not be cached by the context (DbContext or Object Context). This means that the Entity Framework does not perform any additional processing or storage of the entities that are returned by the query. Please note that we cannot update these entities without attaching to the context.

How to use AsNoTracking

The “System.Data.Entity” namespace contains this extension and it is very easy to use in our applications. The following is an example of how to use the AsNoTracking method.

The first query returns all the employees from the database table and we apply the AsNoTrack method on the DbQuery object. The second query returns all the employee rows with an EmployeeId greater than or equal to 3 and here we apply the AsNoTrack method on the IQueryable object.

  1. using(Entities context = new Entities())  
  2. {  
  3.     var employee = context.Employees.AsNoTracking().ToList();  
  4.     var employee2 = context.Employees  
  5.                     .Where(p => p.EmployeeId >= 3)  
  6.                     .AsNoTracking().ToList();  

Query performance with AsNoTracking

The AsNoTracking method 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.

Consider the following example code. Here I am querying an employee entity with and without the AsNoTrack method. To analyze the SQL query and execution time, I am enabling the SQL logging feature of the Entity Framework.
  1. using(Entities context = new Entities())  
  2. {  
  3.     var employeeWith = context.Employees.AsNoTracking().ToList();  
  4.     var employeeWithout = context.Employees.ToList();   

Output





When to use AsNoTracking

As we have seen in the preceding section, there are significant performance gains to be obtained by using the AsNoTrack method. We can get better performance with a Snapshot tracker compared to a proxy tracker. We should use this method with queries in which we do not want to save the data back to the database.

Please note, the preceding result is produced by executing these tests on my machine with a 64bit operating system and it might vary on another machine (it also depends on the physical hardware).

I hope this helps you!

 

Up Next
    Ebook Download
    View all
    test-2
    Read by 1 people
    Download Now!
    Learn
    View all