title: Entity Framework code-first, Many-to-Many relationship on the same table
body: <p>I'm building a social network with Friend function.
  My idea is that I've already had the default ApplicationUser class, so I create a new table called <strong>Friend</strong></p>
  
  <pre><code>public class Friend
  {
      [Key]
      [Column(Order = 1)]
      public string SenderId { get; set; }
  
      [Key]
      [Column(Order = 2)]
      public string ReceiverId { get; set; }
  
      //Status == true : Friend request accepted
      //Status == false : Friend request not accepted
      public bool Status { get; set; }
  }
  </code></pre>
  
  <p>In the <strong>ApplicationUser</strong>, I define 2 navigation properties <em>Senders</em> and <em>Receivers</em> (to link to Friend table)</p>
  
  <pre><code>public class ApplicationUser : IdentityUser
  {
      [Required]
      [StringLength(50)]
      public string Name { get; set; }
  
      [Required]
      public bool Gender { get; set; }
  
      [StringLength(255)]
      public string Address { get; set; }
  
      [StringLength(255)]
      public string Job { get; set; }
  
      [StringLength(255)]
      public string Image { get; set; }
  
      public DateTime Birthday { get; set; }
  
      public ICollection&lt;ApplicationUser&gt; Senders { get; set; }
      public ICollection&lt;ApplicationUser&gt; Receivers { get; set; }
  }
  </code></pre>
  
  <p>And finally in ApplicationDbContext, I declare relationships between 2 tables using Fluent Api</p>
  
  <pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
  
      public DbSet&lt;Friend&gt; Friends { get; set; }
  
      public ApplicationDbContext()
          : base("DefaultConnection", throwIfV1Schema: false)
      {
      }
  
      public static ApplicationDbContext Create()
      {
          return new ApplicationDbContext();
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;ApplicationUser&gt;()
              .HasMany(a =&gt; a.Senders)
              .WithMany(a =&gt; a.Receivers)
              .Map(m =&gt;
              {
                  m.MapLeftKey("ReceiverId");
                  m.MapRightKey("SenderId");
                  m.ToTable("Friends");
              });
          base.OnModelCreating(modelBuilder);
  
      }
  }
  </code></pre>
  
  <p>But when I add-migration, it creates 2 tables like this, and neither of them is what I need (one doesn't have foreign keys, one doesn't have Status properties)</p>
  
  <pre><code>public override void Up()
      {
          CreateTable(
              "dbo.Friends1",
              c =&gt; new
                  {
                      SenderId = c.String(nullable: false, maxLength: 128),
                      ReceiverId = c.String(nullable: false, maxLength: 128),
                      Status = c.Boolean(nullable: false),
                  })
              .PrimaryKey(t =&gt; new { t.SenderId, t.ReceiverId });
  
          CreateTable(
              "dbo.Friends",
              c =&gt; new
                  {
                      SenderId = c.String(nullable: false, maxLength: 128),
                      ReceiverId = c.String(nullable: false, maxLength: 128),
                  })
              .PrimaryKey(t =&gt; new { t.SenderId, t.ReceiverId })
              .ForeignKey("dbo.AspNetUsers", t =&gt; t.SenderId)
              .ForeignKey("dbo.AspNetUsers", t =&gt; t.ReceiverId)
              .Index(t =&gt; t.SenderId)
              .Index(t =&gt; t.ReceiverId);
  
      }
  </code></pre>
  
  <p>What am I supposed to do :( I've searched on internet and this seems legit but it doesn't work</p>