title: "An object with the same key already exists in the ObjectStateManager..." exception is thrown when setting an entity state to modified
body: <p>I followed some examples(including such books as "Pro ASP.NET MVC 3" and "Professional ASP.NET MVC 3") to create simple ASP.NET MVC 3 apps using EF 4.1 (since I'm new to these technologies).</p>
  
  <p>I'm using the following repository(single instance of it is used by all action methods of the controller) to access the DB:</p>
  
  <pre><code>public class ProductRepository : IProductRepository
      {
          private readonly EFDbContext _context = new EFDbContext();
  
          #region Implementation of IProductRepository       
  
         ....
  
          public void SaveProduct(Product product)
           {           
              if (product.ProductId == 0)
              {
                  _context.Products.Add(product);
              }
              else
              {
                  _context.Entry(product).State = EntityState.Modified;
  
              }
  
              _context.SaveChanges();
          }
  
  ....
  }
  </code></pre>
  
  <p><strong>This repository performs updating as it was shown in the examples I used.</strong></p>
  
  <p>Product class:</p>
  
  <pre><code>public class Product
      {       
          public int ProductId { get; set; }       
          public string Name { get; set; }      
          public string Description { get; set; }     
          public decimal Price { get; set; }
          public string Category { get; set; }
  }
  </code></pre>
  
  <p>In case of updating the product, I'm  getting the exception "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key"</p>
  
  <p>I know that the similar questions have been already discussed here but my question is a bit different: </p>
  
  <p>Why this code <strong>which was taken from examples</strong>  is not working (though it looks pretty simple and straightforward)? What wrong might I have done or missed something.</p>