title: EF Core self referential many to many both sides
body: <p>I have an entity that has a many to many onto itself</p>
  <pre><code>public class Docket
  {
      public long Id { get; set; }
      public string Name { get; set; }
  
      public virtual List&lt;DocketDocket&gt; RelateDockets{ get; set; }
  }
  
  
  public class DocketDocket
  {
      public int LeftDocketId { get; set; }
      public Docket.Docket LeftDocket { get; set; }
  
      public int RightDocketId { get; set; }
      public Docket.Docket RightDocket { get; set; }
  }
  </code></pre>
  <p>With the following config</p>
  <pre><code> modelBuilder.Entity&lt;Joins.DocketDocket&gt;().HasKey(t =&gt; new { t.LeftDocketId, t.RightDocketId });            
   modelBuilder.Entity&lt;Joins.DocketDocket&gt;().HasOne(pt =&gt; pt.LeftDocket).WithMany(t =&gt; t.RelatedDockets).HasForeignKey(pt =&gt; pt.LeftDocketId);
   modelBuilder.Entity&lt;Joins.DocketDocket&gt;().HasOne(pt =&gt; pt.RightDocket).WithMany().HasForeignKey(pt =&gt; pt.RightDocketId).OnDelete(DeleteBehavior.Restrict);
  </code></pre>
  <p>I then manually create the link in my repo as such</p>
  <pre><code>await base.Insert(new Joins.DocketDocket() { LeftDocketId = item.Id, RightDocketId = i.RightDocketId });
  </code></pre>
  <p>This works fine but I need this relationship to be double sided so I add the record for the other side</p>
  <pre><code>await base.Insert(new Joins.DocketDocket() { LeftDocketId = i.RightDocketId, RightDocketId = item.Id });
  </code></pre>
  <p>and on this second insert I get</p>
  <p><code>Violation of PRIMARY KEY constraint 'PK_RelatedDockets'. Cannot insert duplicate key in object 'dbo.RelatedDockets'. The duplicate key value is (10791, 10790).</code></p>
  <p>Shouldn't EF have my key as (10790, 10791) for the first entry and then (10791,10790) for the second one and therefore NOT duplicate? If not how can I define a unique key for this type of arrengement?</p>