title: Entity Framework setting an entry to modified throws error
body: <p>While writing an update method for my Entity Framework repository, I include the following code:</p>
  
  <pre><code>public bool UpdateProduct(int id, Models.Product product)
  {
      Product ctxProduct = GetProductIncludingProductLists(id); //Pulls directly from context
      if (ctxProduct != null &amp;&amp; product != null)
      {
          /*Update ctxProduct fields using product*/
  
          _ctx.Entry(ctxProduct).State = EntityState.Modified; //_ctx is my DbContext
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>But the line of code where I set the entity's status to modified throws the following error: </p>
  
  <blockquote>
    <p>A first chance exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll</p>
    
    <p>Attaching an entity of type '<strong><em>.</em></strong>.Product' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.</p>
  </blockquote>
  
  <p>I am confused by what this error message is trying to tell me. Since I have pulled this entry out of the context, the only thing its Primary key should be conflicting with is itself.  In addition, I know this entry has been assigned an id since I accessed it using its id.</p>
  
  <p>Lastly, the reason I am setting this entry's state to modified is because calling <code>_ctx.saveChanges()</code> is returning 0, indicating to me that the context isn't aware I've changed anything (when I have).</p>
  
  <p>Can anyone explain why this error is being thrown and what I need to do to make the context aware of my changes?</p>
  
  <p>EDIT</p>
  
  <p>GetProductIncludingProductLists(id):</p>
  
  <pre><code>    public Product GetProductIncludingProductLists(int id)
      {
          try
          {
              return _ctx.Products.Include("ProductLists")
                                      .ToList()
                                      .Select(p =&gt; new Product()
                                      {
                                          ProductId = p.ProductId,
                                          CUSIP = p.CUSIP,
                                          SEDOL = p.SEDOL,
                                          BUID = p.BUID,
                                          Key = p.Key,
                                          Track = p.Track,
                                          ProductLists = ((p.ProductLists.Select(l =&gt; new ProductList()
                                          {
                                              ProductListId = l.ProductListId,
                                              Name = l.Name,
                                              Products = null
                                          })
                                          .ToList() as List&lt;ProductList&gt;) ?? null)
                                      })
                                      .First(item =&gt; item.ProductId == id);
          }
          catch (Exception e)
          {
              System.Diagnostics.Debug.WriteLine(e.Message);
              return null;
          }
      }
  </code></pre>
  
  <p>The reason for the crazy select statements is because product and product list are related N:N and not including them would cause an error for 'circular serialization'</p>