Chronicling the Adventure that is software Development

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!

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.