Use AsNoTracking method to get data much faster in Entity Framework

Vishal Pathak
Abhima C# Programming
2 min readJan 1, 2022
Photo by Joshua Sortino on Unsplash

Entity Framework is an ORM (Object Relation Mapping) framework. Entity framework is made for ASP.NET applications by Microsoft.

It supports LINQ queries, change tracking, updates, and schema migrations. Entity framework works with many databases, including SQL Database (on-premises and Azure), SQLite, MySQL, MSSQL, PostgreSQL, and Azure Cosmos DB. For more details please visit Entity Framework Documentation .

To know how AsNoTracking method can improve the performance we should know how it does fetch the data. So let’s get jump to that first.

Without using AsNoTracking method in Entity Framework:-

Normally when we don’t use AsNoTracking method in Entity Framework and fetch the data, it retrieve the data and save the data in Cache memory. So when you want to update the retrieved data then only refrain from using the AsNoTracking method in Entity Framework. As when you change the values from retrieved data then it make changes in the cache data and it will push the changes into the DataBase. Below example will show how many mega bytes it take in cache memory to fetch the data without AsNoTracking method.

Use AsNoTracking method to get data much faster in Entity Framework

Output:-

Without using AsNoTracking method: 9.640824mb

So now we come to know that how Entity Framework retrieves the data now let’s understand what AsNoTracking method does.

When we use AsNoTracking method, Entity Framework do not save the data in cache memory because of which Garbage Collector will have less work to do and less memory occupation will also be low. Below example will show how AsNoTracking method takes less memory for the same query.

Use AsNoTracking method to get data much faster in Entity Framework

Output:-

Using AsNoTracking method: 4.435776mb

Now it is visible that almost 50% less memory is getting used by using AsNoTracking method in Entity Framework.

For ASP.NET tutorial go and visit tutorialsteacher.

Bhagavad Gita Verse of the Day

श्रोत्रं चक्षु: स्पर्शनं च रसनं घ्राणमेव च |
अधिष्ठाय मनश्चायं विषयानुपसेवते || 9||

śhrotraṁ chakṣhuḥ sparśhanaṁ cha rasanaṁ ghrāṇam eva cha
adhiṣhṭhāya manaśh chāyaṁ viṣhayān upasevate

BG 15.9: Using the sense perceptions of the ears, eyes, skin, tongue, and nose, which are grouped around the mind, the embodied soul savors the objects of the senses.

--

--

Vishal Pathak
Abhima C# Programming

love ❤ coding, solving some industry problems technologies: JavaScript, C#, Angular, PLSQL, Docker Want to learn: Python, Go language, AI, ML and Cloud