title: Violation of primary key Entity Framework Code First
body: <p>I have started with C# and I wanted to make my own DB.</p>
  
  <p>I have two models</p>
  
  <pre><code>public class AModel 
  {
      public Guid ID { get; private set; }
      public string Name { get; set; }
      public int Count { get; set; }
      public AModel()
      {
          this.ID = Guid.NewGuid();
      }
  }
  
  public class BModel 
  {
      public Guid ID { get; private set; }
      public string Name { get; set; }
      public AModel Model { get; set; }
      public BModel()
      {
          this.ID = Guid.NewGuid();
      }
  }
  </code></pre>
  
  <p>When I try to save BModel to DB, I get this error:</p>
  
  <blockquote>
    <p>Violation of PRIMARY KEY constraint 'PK_dbo.AModels'. Cannot insert
    duplicate key in object 'dbo.AModels'. The duplicate key value is
    (48ee1711-8da4-46c1-a714-19e985211fed).\r\nThe statement has been
    terminated.</p>
  </blockquote>
  
  <p>I thought it would be solved by this</p>
  
  <pre><code>modelBuilder.Entity&lt;BModel&gt;().HasRequired(t =&gt; t.Model).WithMany();
  </code></pre>
  
  <p>but it looks like I am completely lost. Could anybody help me with this simple example?</p>