Entity Framework Core 1.0 – Speed up the retrieval of large data sets with .AsNoTracking()

If you are experiencing performance issues with Entity Framework Core 1.0 when you try to include related objects in your LINQ and lambda expressions on record sets of just a few thousand records, (More about Lazy and Eager Loading here) then check out these examples on how to improve it. Of the many ways, the best way that I have found to negate those performance problems and to optimize your requests is by using .AsNoTracking()

What is AsNoTracking()
When you use .AsNoTracking() you are basically telling the context not to track the retrieved information. Sure, this turns off a useful EF feature, but in many cases (like building an api in my case) this feature is unneeded.

In summary, Tracking is turned on by default and is great if you want to retrieve and save objects all within the same context. But if this is not required, you should use AsNewTracking(). AsNoTracking() decouples the returned records from the context, making your queries much faster!


IList<Player> playerList = db.Player.AsNoTracking().ToList()

Example with Joins

IList<Player> playerList = (from p in db.Player
                        select p)
                        .Include(p => p.Team)
                        .ThenInclude(t => t.League)


It is also worth noting that there is another way to decouple your objects from EF.  This other way can be done by adding .AsEnumerable().  That would look like this:

var decoupledList = from x in TableA<strong>.AsEnumerable()</strong>

4 thoughts on “Entity Framework Core 1.0 – Speed up the retrieval of large data sets with .AsNoTracking()

  1. RijstMetBhaat December 9, 2016 / 3:06 pm

    What if I’m using .NET Core 1.0?

    Entity Framework is not compatible with that.

    Any other way to call AsNoTracking()? Or a workaround?

    Liked by 1 person

    • jesse December 27, 2016 / 5:51 pm

      .Net Core 1.0 does have .AsNoTracking()

      It is contained in the “System.Data.Entity” namespace.


  2. Ed Eaglehouse December 27, 2016 / 4:25 pm

    What’s AsNewTracking()? Does that ignore existing records in the context cache and retrieve a record every time it’s referenced? I’m not familiar with it.


    • jesse December 27, 2016 / 5:48 pm

      If you use AsNoTracking() when querying for a record, once found, that record will not be tracked/attached/linked (how ever you want to say it) to the record in your data context. A new record is not retrieved every time it is referenced, but it has been separated from the context. This provides better performance because without it, Entity Framework will continue to track that record, watching for any changes to it in order to auto update it in your context. It is purely a “Performance Tuning Option” available to us developers that cuts down execution time of large query sets and memory usage.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s