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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:30:46+00:00 2026-06-07T09:30:46+00:00

Can someone explain to me why my EF (4.3) code first code below is

  • 0

Can someone explain to me why my EF (4.3) code first code below is resulting in the “old” password being retrieved.

using (var context = new CableSenseInstanceConfiguratorContext())
{
    var user = context.Installers.Where(u => u.UserName == "admin").FirstOrDefault();
    Console.WriteLine(user.Password); // Outputs "oldpassword"

    // Change the details on a different context;
    using (var context2 = new CableSenseInstanceConfiguratorContext())
    {
        var installer = context2.Installers.Single(i => i.UserName == "admin");
        installer.Password = "changed"; 
        context2.SaveChanges();
    }

    var user2 = context.Installers.Where(u => u.UserName == "admin").FirstOrDefault();
    Console.WriteLine(user2.Password); // Outputs "oldpassword"
}

The password is “oldpassword” to start. So I’m changing the password inside another context (context2), and then fetching it again into user2. I can verify that the output is “oldpassword” for both. From profiling the SQL, I can see that the password does get changed, I can also see that the code that populates user2 IS going to the database, but it’s just not using those values.

I understand that EF has the concept of the Local context as a way of caching and tracking entities, but from what I understand, a context.Installers.Where(..) should force a refetch from the database, whereas a context.Installers.Find() should look in the local context. It seems that no matter how I query Installers, it is using the local cache.

EDIT

Thanks to @Reinard for the solution. I was misunderstanding the docs – I read from here:

Note that DbSet and IDbSet always create queries against the database
and will always involve a round trip to the database even if the
entities returned already exist in the context.

So I assumed that because it would go to the database, it would re-fetch my object. What actually happened is that it went to the database, fetched the object, found I already was tracking that object (because of the previous load), and so I ended up with the old object – this is actually what the docs say!

Using context.Installers.Local.Clear() makes no difference surprisingly, I needed AsNoTracking().

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

    Neither Where or Find will explicitly refetch from the database. As far as I know Find will retrieve the entity from the database only if it doesn’t exist in the context.

    In order to explicitly force a refetch use AsNoTracking().

    e.g.

    context.Installers.AsNoTracking().Where(u => u.UserName == "admin").FirstOrDefault();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to C# programming. Can someone please explain the following code: Console.WriteLine( {0}{1,10},
Can someone explain to me why var divdbl = $(div:first); var i = 0;
Can someone explain the following code snippet for me? // Bind base object so
I am newbie to jQuery, can someone explain what this code does: $(#currency form).submit(function(e)
Can someone explain to me what the bolded portions of this code are doing?
Can someone explain how the mediator pattern works with multiple instances. My code in
Hi can someone explain if the in the following code the synchronized code will
I'm having a problem with TRY...CATCH blocks. Can someone explain why the following code
Can someone write some sample code to explain this concept? I know what a
Can someone please please explain to me why the following code works when I

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.