18

I'm interested in using AsNoTracking with my LINQ select queries to improve performance. I'm using Entity Framework 5 with Code First.

However, all of my queries are written using LINQ Query syntax and all of the AsNoTracking examples are shown using the Method syntax. I'm aware that AsNoTracking was created for the Method syntax but how do I achieve the same thing with the Query syntax?

3
  • AsNoTracking does NOT improve performance, because it lets EF contact the datasource on each call. Tracking is therefore also caching and improves performance. Commented Aug 8, 2013 at 9:45
  • 1
    Hi Robert, that's interesting because I've read a lot of articles that point towards AsNoTracking() giving a performance improvement when using SELECT's without needing to do any updating. ie blog.staticvoid.co.nz/2012/4/2/… Commented Aug 8, 2013 at 9:55
  • Ok, to be precise: AsNoTracking is indeed faster when fetching data that has not been fetched before, because it does not need to check if the desired data is already present (which would be the fastest, of course). Commented Aug 8, 2013 at 10:00

2 Answers 2

33

You apply AsNoTracking() to the DbSet:

var result = (
    from person in ctx.People.AsNoTracking()
    select person)
    .ToList();
Sign up to request clarification or add additional context in comments.

5 Comments

Works but I had to add using Microsoft.EntityFrameworkCore;
what about joins? Do you apply them to those sets as well? EG: from s in _context.Strategy.AsNoTracking() join sv in _context.StrategyVersion.AsNoTracking() on s.Id equals sv.StrategyId
See this answer
Do I need to add this to DbSets tat I include in joins?
@Hooch ^^ it's literally the comment above yours
1

Query syntax is replaced with method syntax by compiler, so there is no difference at all at the end.

2 Comments

I've tried using AsNoTracking() with my Query syntax but it doesn't even show up on Intellisense as an option.
On further thoughts, maybe I'm not using it correctly or I'm missing a library? ie var result = (from person in ctx.People select person).AsNoTracking().ToList()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.