The use and purpose of AsNoTracking method.

During class, accessing the database can have some interesting results that have to be addressed. Below is part of an article by Jignesh Trivedi explaining this a bit.

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. http://www.c-sharpcorner.com/UploadFile/ff2f08/entity-framework-and-asnotracking/

0 COMMENTS

Please Log in or Register to leave a comment