Geeks With Blogs
Juans and Zeros

I was running Entity Framework Profiler on one of my projects using EF5 and came across several queries that were using SELECT TOP (2) and was wondering why that was happening.

There were two different reasons I came across.

Reason 1:  When using Linq SingleOrDefault it will check to see if 2 results are returned so an exception can be thrown.  This is per MSDN http://msdn.microsoft.com/en-us/library/bb342451.aspx.  Instead I switched over to using Linq FirstOrDefault which translates to a SELECT TOP 1 query.

Reason 2:  DbContext.Find().  Find checks first if the entity with the given key is already in the context.  If not it queries the database.  Viewing a stack trace showed that it too is also using Linq SingleOrDefault.

Posted on Thursday, April 18, 2013 4:39 PM EF | Back to top


Comments on this post: Why Entity Framework Profiler is showing SELECT TOP 2 Queries

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Juan | Powered by: GeeksWithBlogs.net