title: EF4.1 Code first one to many self reference
body: <p>I'm trying to make a one to many self reference using EF4.1 code first. My entity looks like this</p>
  
  <pre><code>public class Site
  {
      public int Id { get; set; }
      public int? ParentId { get; set; }
      public string Sitename { get; set; }
  
      public virtual Site ChildSite { get; set; }
      public virtual ICollection&lt;Site&gt; ChildSites { get; set; }
  }
  </code></pre>
  
  <p>And in my context class I do this to make the self reference</p>
  
  <pre><code>modelBuilder.Entity&lt;Site&gt;()
      .HasOptional(s =&gt; s.ChildSite)
      .WithMany(s =&gt; s.ChildSites)
      .HasForeignKey(s =&gt; s.ParentId);
  </code></pre>
  
  <p>But when i try to add some dummy data to my database with this code</p>
  
  <pre><code>var sites = new List&lt;Site&gt;
  {
      new Site { ParentId = 0, Sitename = "Top 1" },
      new Site { ParentId = 0, Sitename = "Top 2" },
      new Site { ParentId = 0, Sitename = "Top 3" },
      new Site { ParentId = 0, Sitename = "Top 4" },
      new Site { ParentId = 1, Sitename = "Sub 1_5" },
      new Site { ParentId = 1, Sitename = "Sub 1_6" },
      new Site { ParentId = 1, Sitename = "Sub 1_7" },
      new Site { ParentId = 1, Sitename = "Sub 1_8" },
      new Site { ParentId = 2, Sitename = "Sub 2_9" },
      new Site { ParentId = 2, Sitename = "Sub 2_10" },
      new Site { ParentId = 2, Sitename = "Sub 2_11" },
      new Site { ParentId = 2, Sitename = "Sub 2_12" },
      new Site { ParentId = 3, Sitename = "Sub 3_13" },
      new Site { ParentId = 3, Sitename = "Sub 3_14" },
      new Site { ParentId = 3, Sitename = "Sub 3_15" },
      new Site { ParentId = 3, Sitename = "Sub 3_16" },
      new Site { ParentId = 4, Sitename = "Sub 4_17" },
      new Site { ParentId = 4, Sitename = "Sub 4_18" },
      new Site { ParentId = 4, Sitename = "Sub 4_19" },
      new Site { ParentId = 4, Sitename = "Sub 4_20" }
  };
  sites.ForEach(s =&gt; context.Sites.Add(s));
  context.SaveChanges();
  </code></pre>
  
  <p>I get this error : </p>
  
  <blockquote>
    <p>Unable to determine the principal end of the
    'Cms.Model.Site_ChildSite' relationship. Multiple added entities may
    have the same primary key.</p>
  </blockquote>
  
  <p>What am i missing here ?</p>
  
  <p><strong>Edit :</strong></p>
  
  <p>Here is the solution to my problem. I removed the <code>public virtual Site ChildSite { get; set; }</code> from the entity</p>
  
  <pre><code>public class Site
  {
      public int Id { get; set; }
      public int? ParentId { get; set; }
      public string Sitename { get; set; }
  
      public virtual ICollection&lt;Site&gt; ChildSites { get; set; }
  }
  </code></pre>
  
  <p>I then changed the <code>modelBuilder</code> to this</p>
  
  <pre><code>modelBuilder.Entity&lt;Site&gt;()
      .HasMany(s =&gt; s.ChildSites)
      .WithOptional()
      .HasForeignKey(s =&gt; s.ParentId);
  </code></pre>
  
  <p>As it seems that auto generation of the database did not work I went ahead and created the table myself like this</p>
  
  <pre><code>int Id, not null, primary key
  int ParentId, null
  string Sitename, null
  </code></pre>
  
  <p>And everything works as I want it to.</p>
  
  <p><strong>2nd Edit</strong></p>
  
  <p>And the final step to get the auto generation of dummy data to work</p>
  
  <pre><code>var parent1 = new Site
  {
      Sitename = "Top 1",
      ChildSites = new List&lt;Site&gt;
          {
              new Site {Sitename = "Sub 1_5"},
              new Site {Sitename = "Sub 1_6"},
              new Site {Sitename = "Sub 1_7"},
              new Site {Sitename = "Sub 1_8"}
          }
  };
  
  ect . . . 
  
  context.Sites.Add(parent1);
  context.SaveChanges();
  </code></pre>
  
  <p>See Slauma answer below</p>