title: Entity Framework Creates New / Duplicate Entries for Associated Objects
body: <p>I am trying to use Code First to create an SQL CE 4 database. When running the sample code below, Entity Framework is inserting new records for product each time, even though the data is exactly the same. <strong>What do I need to do to make Entity Framework not create duplicate associated products?</strong> The values in the ForeignID1 and the Product object are values that already exist in the database, but Entity Framework is wiping the ID I give it and adding a new ID.</p>
  
  <pre><code>namespace MyApp.Model
  {
      public class MyThing
      {    
          public int ID { get; set; }
  
          [ForeignKey("Product")]
          public int ForeignID1{ get; set; }
  
          public virtual Product Product { get; set; }
      }
  }
  
  // Data.DataManager.cs
  
  public class DataManager : DbContext
  {
      public DbSet&lt;Model.MyThing&gt; Things{ get; set; }
      public DbSet&lt;Model.Product&gt; Products { get; set; }
  }
  </code></pre>
  
  <p>These are the values it has entered. There should only be one value in the table that is referenced by multiple <code>MyThings</code>'s</p>
  
  <p><img src="https://i.stack.imgur.com/kpn0Q.png" alt="enter image description here"></p>