title: Violation of PRIMARY KEY constraint 'PK_dbo.AmazonProducts'. Cannot insert duplicate key in object
body: <p>I have a problem trying to save an entity which is a foreign key to a parent object..</p>
  
  <p>i have an amazonProduct entity.  And an AmazonCompetitivePrice entity which is a virtual list on the amazonProduct like so :</p>
  
  <pre><code>public class AmazonProduct
  {
          [Key]
          [DatabaseGenerated(DatabaseGeneratedOption.None)]
          public virtual int ASIN
  
          public virtual IList&lt;AmazonProductCompetitivePrice&gt; amazonProductCompetitivePrices = new List&lt;AmazonProductCompetitivePrice&gt;();
  }
  </code></pre>
  
  <p>So i have a AmazonProduct which i retrieve from the database, and then add a new AmazonProductCompetitivePrice to the amazonProduct. </p>
  
  <p>But when i try save this, I get the following error:  </p>
  
  <blockquote>
    <p>Violation of PRIMARY KEY constraint 'PK_dbo.AmazonProducts'. Cannot
    insert duplicate key in object 'dbo.AmazonProducts'.\r\nThe statement
    has been terminated</p>
  </blockquote>
  
  <p>It looks like its not realising that my AmazonProduct is already in the database, and its trying to save a new one but the primary key already exists!  </p>
  
  <p>I used the fluent API to map the foreign key like so:</p>
  
  <pre><code>        modelBuilder.Entity&lt;AmazonProduct&gt;()
                      .HasMany(pl =&gt; pl.AmazonProductCompetitivePrices)
                      .WithOptional(p =&gt; p.AmazonProduct)
                      .Map(c =&gt; c.MapKey("ASIN"));
  </code></pre>
  
  <p>Anyone know whats wrong with this?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT:</strong>
  object retrieval:</p>
  
  <pre><code> using (var uow = new UnitOfWorkInventory())
              {
                  using (var amazRepo = new AmazonProductRepository(uow))
                  {
                      return amazRepo.FindByAsin(ASIN);
                  }
              }
   public AmazonProduct FindByAsin(string asin)
          {
              return context.AmazonProducts.Include(x =&gt; x.AmazonLowestOfferListings).Include(x =&gt; x.AmazonMyPrices).Include(x =&gt; x.AmazonProductCompetitivePrices).SingleOrDefault(x =&gt; x.ASIN == asin);
          }
  </code></pre>
  
  <p>That gets me the AmazonProduct.. then the save:</p>
  
  <pre><code>using (var uow = new UnitOfWorkInventory())
  {
       using (var amazonRepo = new AmazonProductCompetitivePriceRepository(uow))
       {
            amazonProductCompetitivePrice.AmazonProduct = amazonProduct;
            amazonRepo.InsertOrUpdate(amazonProductCompetitivePrice);
       }
            uow.Commit();
  }
  
    public void InsertOrUpdate(AmazonProductCompetitivePrice amazonProductCompetitivePrice)
      {
          if (amazonProductCompetitivePrice.Id == default(int))
          {
              // New entity
              context.AmazonProductCompetitivePrices.Add(amazonProductCompetitivePrice);
          }
          else
          {
              // Existing entity
              context.Entry(amazonProductCompetitivePrice).State = EntityState.Modified;
          }
      }
  </code></pre>
  
  <p>Thats everything.. and thanks for the help!!</p>