title: EF4 Object State Manager is claiming multiple entries are attached when there aren't
body: <p>I am trying to add functionality in my ASP.NET MVC app to change some fields of my data structure.  After clicking the submit button on the form, the following action is called:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Edit(Document doc)
      {
          // Attempt to save the project
          if (_documentService.SaveDocument(doc) == ServiceActionResult.Success)
              return RedirectToAction("List");
          else
              return View();
      }
  </code></pre>
  
  <p>The <code>SaveDocument()</code> method looks like this:</p>
  
  <pre><code>    public ServiceActionResult SaveDocument(Document doc)
      {
          if (doc == null)
              return ServiceActionResult.ActionFailure;
  
          // Check if this is a new Document (null ID)
          if (doc.Id == 0)
              _documentRepository.Add(doc);
          else
              _documentRepository.Attach(doc);
  
          _documentRepository.SaveChanges();
  
          return ServiceActionResult.Success;
      }
  </code></pre>
  
  <p>Since the document exists (and thus has an Id value), I call the <code>Attach()</code> method of my generic repository.  The attach method just looks like:</p>
  
  <pre><code>    public void Attach(T entity)
      {
         _objectSet.Attach(entity);
      }
  </code></pre>
  
  <p>When the object set's <code>Attach()</code> method is called, the following exception occurs:</p>
  
  <pre><code>An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
  </code></pre>
  
  <p>I don't understand why this is happening, since as you can see the whole lifecycle of the request only has one call to EF, and that's the final <code>Attach(entity)</code> call.  Am I missing something?</p>
  
  <p>As an FYI, this system used to work, but what seems to have broke it was my conversion from model first to code first with POCOs (latest CTP).  All my other functionality works just as it did before, except for this scenario.</p>
  
  <p>If it helps, my generic repository gets the <code>ObjectSet</code> from the <code>DbContext</code> via a function I wrote in my context class:</p>
  
  <pre><code>    public ObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() where T : class
      {
          return ObjectContext.CreateObjectSet&lt;T&gt;();
      }
  </code></pre>
  
  <p><hr>
  <b>Edit:</b>
  After looking around my code due to the comments I just remembered that I had implemented a singleton for context management.  I did this due to the fact that it's possible a Controller may get entities from one service class, and use another service class to update it (I think that was the scenario I was trying to solve, I implemented it a while ago and I'd have to look closer when I get off of work).  </p>
  
  <p>I used a singleton for this because I had the impression that the singleton wouldn't go across multiple http requests, and I assume by the comments that that could be the issue.  I'll look at this when I get home.</p>