title: DbUpdateException on SaveChanges if child foreign key already exists - in Entity Framework Code First
body: <p>[ <em>Using Code First DbContext with Entity Framework 5.0 RC</em> ]</p>
  
  <h3>Entity with 2 Navigational Properties / 2 Foreign Keys</h3>
  
  <pre><code>public class Compositon
  {
  
      public string Id { get; set; }
  
      public string SimpletonId { get; set; }
  
      [ForeignKey("SimpletonId")]
      public Simpleton Simpleton { get; set; }
  
      public string CompanitonId { get; set; }
  
      [ForeignKey("CompanitonId")]
      public Companiton Companiton { get; set; }
  }
  </code></pre>
  
  <h3>First Pass - SaveChanges to Empty Database Works</h3>
  
  <pre><code>var composition = new Compositon();
  compositon.Id = "UniquePrimaryKey";
  var simpleton = new Simpleton();
  // This foreign key does not exist in database yet
  simpleton.Id = "Simpleton1";
  composition.Simpleton = simpleton;
  var companiton = new Companiton();
  companiton.Id = "SomeOther1";
  composition.Companiton = companiton;
  // Repositor references the DbContext
  Repositor.Compositons.Add(composition);
  Repositor.SaveChanges();
  </code></pre>
  
  <h3>Second Pass - Existing Child Foreign Key Leads to Parent Fault</h3>
  
  <pre><code>var composition = new Compositon();
  compositon.Id = "AnotherUniquePrimaryKey";
  var simpleton = new Simpleton();
  // This foreign key already exists in database
  simpleton.Id = "Simpleton1";
  composition.Simpleton = simpleton;
  var companiton = new Companiton();
  companiton.Id = "SomeOther2";
  composition.Companiton = companiton;
  Repositor.Compositons.Add(composition);
  Repositor.SaveChanges();
  </code></pre>
  
  <p><code>DbUpdateException: An error occurred while updating the entries.</code></p>
  
  <p><em>I need to be able save these parent classes into the database, because they are unique even though they sometimes contain a navigational property that is already stored - how can I save the parent from this child primary key conflict?</em></p>