title: Self Referencing Many-to-Many relations
body: <p>I have an <code>Ticket</code> entity:</p>
  
  <pre><code>    public class Ticket
      { 
          public int Id { get; set; }
          public string Title { get; set; }
  
          public virtual ICollection&lt;Relation&gt; RelatedTickets { get; set; }
      }
  </code></pre>
  
  <p>I want to setup many-to-many self-relations in Entity Framework Core, so i made two one-to-many relations:</p>
  
  <pre><code>public class Relation
  {
      [Required, ForeignKey("TicketFrom")]
      public int FromId { get; set; }
  
      [Required, ForeignKey("TicketTo")]
      public int ToId { get; set; }
  
      public virtual Ticket TicketFrom { get; set; }
      public virtual Ticket TicketTo { get; set; }
  }
  </code></pre>
  
  <p>I've tried to create the relationship using fluent API:</p>
  
  <pre><code>        builder.Entity&lt;Relation&gt;()
                 .HasKey(uc =&gt; new { uc.FromId, uc.ToId });
          builder.Entity&lt;Relation&gt;()
             .HasOne(c =&gt; c.TicketFrom)
             .WithMany(p =&gt; p.RelatedTickets)
             .HasForeignKey(pc =&gt; pc.FromId);
          builder.Entity&lt;Relation&gt;()
             .HasOne(c =&gt; c.TicketTo)
             .WithMany(p =&gt; p.RelatedTickets)
             .HasForeignKey(pc =&gt; pc.ToId);
  </code></pre>
  
  <p>But in result i have an error:</p>
  
  <blockquote>
    <p>Cannot create a relationship between 'Ticket.RelatedTickets' and
    'Relation.TicketTo', because there already is a relationship between
    'Ticket.RelatedTickets' and 'Relation.TicketForm'.  Navigation
    properties can only participate in a single relationship.</p>
  </blockquote>
  
  <p>The possible solution is to add Parent relation directly to <code>TicketEntity</code>:</p>
  
  <pre><code>public class Ticket
  { 
      public int Id { get; set; }
  
      [Required, ForeignKey("ParentRelation")]
      public Nullable&lt;int&gt; ParentRelationId { get; set; }
  
      public virtual Ticket ParentRelation {get;set;}
  
      public virtual ICollection&lt;Ticket&gt; RelatedTickets { get; set; }
      ...
  }
  </code></pre>
  
  <p>With fluent api like this:</p>
  
  <pre><code>modelBuilder.Entity&lt;Ticket&gt; =&gt;
  {
      entity
          .HasMany(e =&gt; e.RelatedTickets)
          .WithOne(e =&gt; e.ParentRelation) 
          .HasForeignKey(e =&gt; e.ParentRelationId );
  });
  </code></pre>
  
  <p>But it looks 'dirty' to store parent relation like this.<br>
  What is the right approach?</p>