title: Entity Framework: Why does the database get hit if the data is in the context?
body: <p>Why would the database be hit to find a record that is already represented in the ObjectContext?</p>
  
  <p>So here's what I thought would happen when you query:</p>
  
  <p><code>
     SiteUser someUser = context.SiteUser.Where(role => role.UserID == 1).ToList()[0];
  </code></p>
  
  <p>Junk example but the idea is that I want to get a user from the table with the id of 1.  Now assume this is the first time through so what I would guess is that it has to create the SiteUser list on the context, query the database, and then fill it's list.  Using profiler I see this:</p>
  
  <pre><code>SELECT 
   [Extent1].[UserID] AS [UserID], 
   [Extent1].[MainEmail] AS [MainEmail], 
   [Extent1].[Password] AS [Password], 
   [Extent1].[UserName] AS [UserName]
  FROM [TIDBA].[TI_USER] AS [Extent1]
  WHERE 1 = [Extent1].[UserID]
  </code></pre>
  
  <p>Beautiful.  It did what I expect and in the SiteUser list (if I dig far enough using Watch) I can see that there is one item in the context SiteUser list and it happens to be the hydrated object that represents this data row.</p>
  
  <p>Next I want to change something without saving:</p>
  
  <pre><code>someUser.UserName = "HIHIHI";
  </code></pre>
  
  <p>Now say for some reason I want grab it again Using the same context (This is a weird example but it's actually a test so I could prove this happening) :</p>
  
  <pre><code>someUser = context.SiteUser.Where(role =&gt; role.UserID == 1).ToList()[0];
  </code></pre>
  
  <p>What I think would happen is it would look at the SiteUser list on the context since that's what the generated property says. (If not null, return list)  Then it would look to see if it's there and return it.  No database hit.  Guess what profiler says...</p>
  
  <pre><code>SELECT 
   [Extent1].[UserID] AS [UserID], 
   [Extent1].[MainEmail] AS [MainEmail], 
   [Extent1].[Password] AS [Password], 
   [Extent1].[UserName] AS [UserName]
  FROM [TIDBA].[TI_USER] AS [Extent1]
  WHERE 1 = [Extent1].[UserID]
  </code></pre>
  
  <p>Hrm.  Ok so I start thinking that maybe it's a gut check to see if anything has changed on that data item and update the SiteUser object ONLY on values I haven't changed on the client.  (Sort of like context.Refresh(RefreshMode.ClientWins, context.SiteUser) )  So I have it stopped at the :</p>
  
  <pre><code>someUser = context.SiteUser.Where(role =&gt; role.UserID == 1).ToList()[0];
  </code></pre>
  
  <p>Line and I change a value in the database (Password column) and let it hit the database.  Nothing changed on the object.</p>
  
  <p>Something doesn't seem right here.  It hits the database to select the object I already have hydrated in the context yet it doesn't apply the change I manually made in the database.  Why is it hitting the database at all then?</p>
  
  <p><strong>UPDATE</strong>
  Thanks to some links below, I was able to dig in a bit and find this:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/system.data.objects.mergeoption.aspx" rel="nofollow noreferrer">Merge Option</a></p>
  
  <p>Looks like there is an enumeration that is set to tell how to deal with loads.  Now after reading that I saw this for MergeOption.AppendOnly:</p>
  
  <blockquote>
    <p>Objects that already exist in the
    object context are not loaded from the
    data source. This is the default
    behavior for queries or when calling
    the Load method on an
    EntityCollection&lt;(Of &lt;(TEntity>)>).</p>
  </blockquote>
  
  <p>This would suggest that if I have it in the context, there should be no hit to the database.  However, this doesn't seem to be true.  It would make sense if OverwriteChanges or PreserveChanges were the defaults, but they are not.  This seems to be contradictory to what is supposed to happen.  Only thing I can think of is that "loaded" just means there are no overwrites.  However, it doesn't mean there are no queries to the database.</p>