'How can i configure a many-to-many relation (friendship between two Users) on the same table with EF Core?

Im actually working on a Social Network web app with ASP.NET(6.0) MVC. Then i found out i will have to set up a many-to-many relations between users to modelize friendship... I made a lot of researches but i kept finding many-to-many relationship between different models. But in my case i had to work with the same model so here i am and really need help.

Here's my actual User model

public class User
    {


        [Key]
        public int UserId { get; set; }

        [Required(ErrorMessage = "Email is required")]
        [EmailAddress]
        public string Email { get; set; }

        [Required(ErrorMessage = "Password is required")]
        [DataType(DataType.Password)]
        public string Password { get; set; }
        [Required(ErrorMessage = "Username is required")]
        public string UserName { get; set; }
        public string Gender { get; set; }
        public string Phone { get; set; }
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [DataType(DataType.Date)]
        public System.DateTime BirthDate { get; set; }
        public string About { get; set; }

        public virtual ICollection<User> Friendship { get; set; }
        public virtual ICollection<Post> Posts { get; set; }


        public User() { }

        public User(string email, string password, string uname, string gender, string phone, System.DateTime bdate, string about)
        {
            this.Email = email;
            this.Password = password;
            this.UserName = uname;
            this.Gender = gender;
            this.Phone = phone;
            this.About = about;

            this.Friendship = new List<User>();
            this.Posts = new List<Post>();
        }
    }

Then i tried to create a "Friendship" model as a record table

public class Friendship
    {
        public int UserId { get; set; }
        public int FriendId { get; set; }
        public virtual User User { get; set; }
        public virtual User Friend { get; set; }
    }

then i supposed the "User" user would be the one who invite the "Friend" user



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source