What difference does .AsNoTracking() make?

207,655

Solution 1

The difference is that in the first case the retrieved user is not tracked by the context so when you are going to save the user back to database you must attach it and set correctly state of the user so that EF knows that it should update existing user instead of inserting a new one. In the second case you don't need to do that if you load and save the user with the same context instance because the tracking mechanism handles that for you.

Solution 2

see this page Entity Framework and AsNoTracking

What AsNoTracking Does

Entity Framework exposes a number of performance tuning options to help you optimise the performance of your applications. One of these tuning options is .AsNoTracking(). This optimisation allows you to tell Entity Framework not to track the results of a query. This means that Entity Framework performs no additional processing or storage of the entities which are returned by the query. However, it also means that you can't update these entities without reattaching them to the tracking graph.

there are significant performance gains to be had by using AsNoTracking

Solution 3

No Tracking LINQ to Entities queries

Usage of AsNoTracking() is recommended when your query is meant for read operations. In these scenarios, you get back your entities but they are not tracked by your context.This ensures minimal memory usage and optimal performance

Pros

  1. Improved performance over regular LINQ queries.
  2. Fully materialized objects.
  3. Simplest to write with syntax built into the programming language.

Cons

  1. Not suitable for CUD operations.
  2. Certain technical restrictions, such as: Patterns using DefaultIfEmpty for OUTER JOIN queries result in more complex queries than simple OUTER JOIN statements in Entity SQL.
  3. You still can’t use LIKE with general pattern matching.

More info available here:

Performance considerations for Entity Framework

Entity Framework and NoTracking

Solution 4

Disabling tracking will also cause your result sets to be streamed into memory. This is more efficient when you're working with large sets of data and don't need the entire set of data all at once.

References:

Solution 5

AsNoTracking() allows the "unique key per record" requirement in EF to be bypassed (not mentioned explicitly by other answers).

This is extremely helpful when reading a View that does not support a unique key because perhaps some fields are nullable or the nature of the view is not logically indexable.

For these cases the "key" can be set to any non-nullable column but then AsNoTracking() must be used with every query else records (duplicate by key) will be skipped.

Share:
207,655

Related videos on Youtube

dotnetnoob
Author by

dotnetnoob

I'm work as a self employment online marketeer, and have recently decided to upgrade my skills so that I can accomplish more with my own websites. I tend to ask a lot of questions, and being a noob I guess they'll be pretty basic, so I should be a great source of reputation points :)

Updated on July 05, 2022

Comments

  • dotnetnoob
    dotnetnoob almost 2 years

    I have a question regarding the .AsNoTracking() extension, as this is all quite new and quite confusing.

    I'm using a per-request context for a website.

    A lot of my entities don't change so don't need to be tracked, but I have the following scenario where I'm unsure of what's going to the database, or even whether it makes a difference in this case.

    This example is what I'm currently doing:

    context.Set<User>().AsNoTracking()
    // Step 1) Get user
    context.Set<User>()
    // Step 2) Update user
    

    This is the same as above but removing the .AsNoTracking() from Step 1:

    context.Set<User>();
    // Step 1) Get user
    context.Set<User>()
    // Step 2) Update user
    

    The Steps 1 & 2 use the same context but occur at different times. What I can't work out is whether there is any difference. As Step 2 is an update I'm guessing both will hit the database twice anyway.

    Can anyone tell me what the difference is?

  • Ghost
    Ghost over 7 years
    It seems that the gains may be counterbalanced sometimes: stackoverflow.com/questions/9259480/…
  • Dilhan Jayathilake
    Dilhan Jayathilake over 7 years
    Can we get the same benefits for anonymous classes in select query, such as context.Users.Select(u=> new { Name = u.Name })? Thanks.
  • Ladislav Mrnka
    Ladislav Mrnka about 7 years
    @DilhanJayathilake: Anonymous classes don't represent the entity itself so they don't have tracking.
  • crokusek
    crokusek over 6 years
    Since EF6 infers the entity key incorrectly on a View sometimes, does AsNoTracking() ignore the key and therefore be an alternative to manually fixing the key (assuming other benefits of the key are not needed).
  • Karl
    Karl over 6 years
    My performance gain with a complex query loading a parent child relationship with include in one step was about 50%
  • Ade
    Ade about 6 years
    Just to reiterate the importance of this with Views, I have a query from a view which returns 7 unique records when run via SSMS. When run via EF, without the AsNoTracking modifier, I get the first record, three copies of the second and three copies of the third. This took a lot of incredulous head-scratching to fix, and it was using AsNoTracking which fixed it!
  • Douglas Gaskell
    Douglas Gaskell almost 6 years
    Also note, the biggest effect AsNoTracking has is that lazy loading will not work
  • red_dorian
    red_dorian over 5 years
    I had this exact same issue when using Linq to Entities while querying a View with no primary keys. Only found out about AsNoTracking after half a day of head-scratching. This ASP.Net forum post eventually led me to it. forums.asp.net/t/…
  • kofifus
    kofifus over 2 years
    "You still can’t use LIKE with general pattern matching" - I can't find any official reference for this and it does seem to work fine for me
  • Mick
    Mick almost 2 years
    @LadislavMrnka isn't quite correct. If your anonymous type includes instances of Entities that can be tracked, EF will track the entities within the anonymous type. So AsNoTracking() can be applicable for an anonymous type.