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 8133109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:30:43+00:00 2026-06-06T09:30:43+00:00

I have a single data context (created once), and I use it to get

  • 0

I have a single data context (created once), and I use it to get the same database record several times, for example, I get the same product record specifying the same product ID.

Each time I call, let’s say, context.Products.Single(p => p.ProdID == 1), context returns the same instance of the product entity, BUT queries the database each time, which is visible in SQL Server Profiler.

I check that it returns the same instance by modifying some properties between the calls. The next call returns the instance with the modified properties.

Why it queries database each time, and is there a way to avoid this?

  • 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-06-06T09:30:44+00:00Added an answer on June 6, 2026 at 9:30 am

    You can use ObjectContext.GetObjectByKey (DbSet<T>.Find for DbContext) to query for an entity by its key. This method will check if the object with the specified key is already attached to the context. If yes, it will return that object without querying the database. If it doesn’t exist in the context it runs a database query.

    LINQ queries generally query the database, no matter if the objects are already attached to the context or not. EF cannot know up front if the objects are attached or not because it doesn’t know the result of the query. Your LINQ query by key is an exception – theoretically – because EF could know if the query is necessary or not since it could check if the object with that key is already attached. But this exception is not handled as such – that’s why GetObjectByKey exists – and it queries the database like every other LINQ query.

    What happens with the query result is determined by the ObjectSet’s MergeOption. The default is AppendOnly which means that the query result is thrown away if an object with the key is already attached to context. Nothing gets overwritten or refreshed by the query. Only if an object with the key doesn’t exist in the context an object from the query result is materialized and will be attached to the context. You can change this default behaviour by choosing another MergeOption.

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

Sidebar

Related Questions

I have a simple database named customer with a single table data.I want to
I have created a method in my data context that is mapped to a
I have a single xml document (data.xml), which I display as HTML using an
The javascript code I now have can toggle a single row of data. Whether
I have a Data class that subclasses NSManagedObject with a single bool property attribute.
I have a data storage requirement which is an excellent candidate for single instance
I have a file containing data in a single column .. I have to
I have the scenario where the data from a single table must be in
I have a small data collection app which has a single button to start
I have a JSP page retrieving data and when single or double quotes are

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.