title: Attaching Entities when using ObjectContext having lifetime of Http Request
body: <p>I'm using .NET 3.5 SP1 in ASP.NET MVC application. </p>
  
  <p>While using ObjectContext with Http Request lifetime, and trying to attach an entity ALREADY present in context, we get error:
  <em>"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."</em>
  For Example, the code : </p>
  
  <pre><code>Category newCategory = new Category {CategoryId = CategoryIdSelected};
  ctx.AttachTo("CategorySet", newCategory);
  </code></pre>
  
  <p>will give error if 'Category' with CategoryId = CategoryIdSelected exists in ObjectContext.</p>
  
  <p>Modified code to check for existing entity:</p>
  
  <pre><code>Category newCategory = new Category {CategoryId = CategoryIdSelected};
  ObjectStateEntry stateEntry = null;
  
  if( ctx.ObjectStateManager.TryGetObjectStateEntry(newCategory, out stateEntry)){
     //EntityObject already attached in context, get it
      newCategory = (EntityObject)stateEntry.Entity;
  }else{
      ctx.AttachTo("CategorySet", newCategory);   
  }
  </code></pre>
  
  <p>The modified code is still giving same error:
  <em>"[System.InvalidOperationException] = {"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."</em></p>
  
  <p>Please advise ?</p>
  
  <p>Thank You</p>
  
  <hr>
  
  <p><strong>QUESTION ADDENDUM:</strong></p>
  
  <p>More problems attaching Entities when using ObjectContext having lifetime of Http Request.</p>
  
  <p>For Example, if we have 'AppUser','Category' and Department entities.</p>
  
  <pre><code>public class AppUser : System.Data.Objects.DataClasses.EntityObject{   
   public int Uid {get; set;}   
   public string UserName {get; set;}   
   public string Password {get; set;}   
   public Department Dept {get; set;}   
   public Category catg {get; set;}
   ...........   
  }
  </code></pre>
  
  <p>AppUser has relationship with Department and Category Entities.
  Now when trying to attach 'user':</p>
  
  <pre><code>user = new AppUser{Uid=1,catg = new Category {categoryId=10}, Dept = new Department{departmentId=101}, ...}
  var key = ctx.CreateEntityKey("AppUserSet", user);
  if (ctx.ObjectStateManager.TryGetObjectStateEntry(key, out stateEntry)) {
  </code></pre>
  
  <p>will work ONLY if, in context :</p>
  
  <ul>
  <li>there is NO Category with categoryId=10, and</li>
  <li>there is NO Department with departmentId=101</li>
  </ul>
  
  <p>One option, is to ensure context does not have attached entities by always retrieving using NOMERGE NoTracking option. BUT I found following problems with MergeOption.NoTracking:</p>
  
  <ul>
  <li>Second call would still result in db hit</li>
  <li>You don't get EntityKeys on EntityRefs. So EntityKey of XXXReference is null,which means NO FK Stub. <a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/7492f76f-7e04-46da-a54d-26c06f1872a1" rel="nofollow noreferrer">Please see</a>.<br>
  <strong>How to get EntityKey of Reference w/o loading both ends (both entities)?</strong></li>
  <li>Even though Entity are Detached, they have a reference to the DataContext (via entity._realtionships._context).  <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=507482" rel="nofollow noreferrer">Please see.</a></li>
  </ul>
  
  <p><strong>Please advise.</strong></p>
  
  <p>Thank You.</p>