Coder Perfect

What is the difference between.AsNoTracking() and.AsTracking()?


As this is all new and unclear to me, I have a question about the.AsNoTracking() extension.

For a website, I’m using a per-request context.

Many of my entities don’t change, so they don’t need to be tracked, but I have the following scenario where I’m not sure what’s going to the database, or even if it matters.

This is an example of what I’m doing right now:

// Step 1) Get user
// Step 2) Update user

This is the same as the previous step, but without the.AsNoTracking():

// Step 1) Get user
// Step 2) Update user

Steps 1 and 2 share the same context, however they happen at distinct times. What I can’t figure out is whether or not there is a distinction. Because Step 2 is an update, I expect both to hit the database twice.

I’m not sure what the distinction is.

Asked by dotnetnoob

Solution #1

The distinction is that in the first scenario, the retrieved user is not tracked by the context, thus when saving the user back to the database, you must attach it and set the user’s state correctly so that EF understands that it should update current users rather than inserting new ones. If you load and save the user with the same context instance in the second situation, you won’t have to do anything because the tracking system will take care of it.

Answered by Ladislav Mrnka

Solution #2

look at this page AsNoTracking and Entity Framework

Entity Framework provides a number of performance tuning tools to aid in the optimization of your applications’ performance. The is one of these tuning choices. AsNoTracking(). You can tell Entity Framework not to track the results of a query with this optimization. Entity Framework does not do any additional processing or storage of the entities returned by the query. However, you won’t be able to update these things unless you reattach them to the tracking graph.

Using AsNoTracking can result in considerable speed improvements.

Answered by Moji

Solution #3

LINQ to Entities queries are not tracked.

When your query is intended for read operations, AsNoTracking() is advised. You get your entities back in these instances, but they are not monitored by your context. This guarantees that memory utilization is kept to a minimum and that performance is maximized.

More information can be found here:

Entity Framework performance considerations

NoTracking and Entity Framework

Answered by NullReference

Solution #4

When you disable tracking, your result sets will be streamed into memory. When working with big collections of data and not needing the complete set at once, this is more efficient.


Answered by Ronnie Overby

Solution #5

The “unique key per record” restriction in EF can be avoided with AsNoTracking() (not mentioned explicitly by other answers).

When reading a View that doesn’t have a unique key because certain fields are nullable or the view’s nature isn’t logically indexable, this is tremendously useful.

In these circumstances, the “key” can be any non-nullable column, but AsNoTracking() must be used with every query, or duplicate records (by key) will be skipped.

Answered by crokusek

Post is based on