title: C# Model User, Friend Requests and Friends with Entity Framework Code First
body: <p>I have previously asked a question here on SO about a similar topic but have since taken a different approach. This is my model:</p>
  
  <pre><code>public class ApplicationUser : IdentityUser&lt;int, CustomUserLogin, CustomUserRole,CustomUserClaim&gt;
  {   
      public ApplicationUser()
      {
          Friends = new List&lt;Friend&gt;();
      }
  
      [Required]
      public string Alias { get; set; }
  
      public virtual ICollection&lt;Friend&gt; Friends { get; set; }
  }
  
  public class Friend
  {
      public virtual int Id { get; set; }
  
      public virtual ApplicationUser RequestedBy { get; set; }
  
      public virtual ApplicationUser RequestedTo { get; set; }
  
      public DateTime? RequestTime { get; set; }
  
      public FriendRequestFlag FriendRequestFlag { get; set; }
  }
  
  public enum FriendRequestFlag
  {
      None,
      Approved,
      Rejected,
      Blocked,
      Spam
  };
  </code></pre>
  
  <p>I can add Friends with this approach and they populate when I get them from database, sample:</p>
  
  <pre><code>public void AddFriendRequest(ApplicationUser user, ApplicationUser friendUser)
  {
      var friendRequest = new Friend()
      {
          RequestedBy = user,
          RequestedTo = friendUser,
          RequestTime = DateTime.Now,
          FriendRequestFlag = FriendRequestFlag.None
      };
  
      user.Friends.Add(friendRequest);
  }
  </code></pre>
  
  <p>When I run the code above, the friend table in the database looks like this after execution:</p>
  
  <p><a href="https://i.stack.imgur.com/aRzoA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aRzoA.png" alt="SQL"></a></p>
  
  <p>When I get a user I would wan't Entity Framework to get all rows in Friend were the user is either <code>RequestedBy</code> or <code>RequestedTo</code>. Is this possible to do with EF? For example with Fluent API? I would also like to map the keys so that <code>[ApplicationUser_Id]</code> is not needed.</p>