Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6709775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:55:19+00:00 2026-05-26T07:55:19+00:00

Why does the Entity Framework’s DbContext.Find() generate a query with select top 2 and

  • 0

Why does the Entity Framework’s DbContext.Find() generate a query with select top 2 and a derived table? By definition, the query is looking up by primary key which should be unique.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:55:20+00:00Added an answer on May 26, 2026 at 7:55 am

    Find checks first if the entity with the given key is already in the context. If not it queries the database. Possibly it uses in this case a LINQ query using SingleOrDefault. SingleOrDefault translates to SELECT TOP 2 to be able to throw an exception if the result has more than one entity.

    So, why doesn’t Find use FirstOrDefault (which would translate to SELECT TOP 1). I don’t know, but I would guess that Find wants to check that the entity is really unique in the database. It should – because it’s the primary key the query uses – but model and database could be out of sync because someone changed the primary key in the database, for example: added a column to a composite key in the database but not in the model.

    Really just a hypothesis. Only EF development team probably can answer what’s exactly the reason.

    Edit

    If I do this as described above (add column to composite key in DB and add a record with the same value in the first key column) and call then Find, I get the exception…

    Sequence contains more than one element

    …and this stacktrace:

    //...
    System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
    System.Data.Entity.Internal.Linq.InternalSet`1.FindInStore(
        WrappedEntityKey key, String keyValuesParamName)
    System.Data.Entity.Internal.Linq.InternalSet`1.Find(Object[] keyValues)
    System.Data.Entity.DbSet`1.Find(Object[] keyValues)
    

    So, it looks that Find indeed uses SingleOrDefault.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The .NET Entity framework is giving me the following error: The table/view 'Foo.dbo.vwFoo' does
I have run into a scenario where Entity Framework 4.0 does not generate an
Does anyone know if there's a speed difference between a query with Entity Framework
Why does entity framework generate Create method for entities that takes parameter for the
Im looking for otimization. When I call Count method in Entity Framework, does it
Dumb question I'm sure, but why does Entity Framework EntityDataSource object require the where
Any ideas why does the entity framework in LINQ gives following strange error: Unable
Does anyone know of any good tutorials on ADO.NET Entity Framework? There are a
When using the Entity Framework, does ESQL perform better than Linq to Entities? I'd
What does one loose by creating POCO using T4 templates in entity framework 4.0?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.