title: Turn off tracking in entity framework model first
body: <p>I'm tring to receive an entity and then update it, but I want to get it with no tracking, so I can attach it back to the context.</p>
  
  <p>I have the <code>EntityFramework.dll</code> referenced (4.1). I generated the database from the model. (not code-first).</p>
  
  <p>Get user:</p>
  
  <pre><code>db.Users.MergeOption = MergeOption.NoTracking;
  IQueryable&lt;User&gt; query = db.Users;//.AsNoTracking(); //&lt;-- apparently, this is code-first only.
  
  return query;
  </code></pre>
  
  <p>Update user:</p>
  
  <pre><code>db.Users.Attach(user); //error here.
  ObjectStateEntry entry = db.ObjectStateManager.GetObjectStateEntry(user);
  entry.SetModifiedProperty(propertyName);
  db.SaveChanges();
  return user;
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.</p>
  </blockquote>
  
  <p>I call the method like this:</p>
  
  <pre><code>var user = userRepository.GetUsers().FirstOrDefault(u =&gt; u.UserId == userId);
  user.Identifiers.Add(someIdent);
  userRepository.UpdateUser(user);
  </code></pre>