It's not possible. You can't use a List<UserFriendship>
in User
class either because sometimes a User is a user and other times a User is a friend, unless you have duplicates in your UserFriendship
table like this:
Friendship table: |
----------------------------
User / Friend
-------------
A / B
B / A
-------------
First change User
and UserFriendship
classes as follows:
class UserFriendship
{
public Guid FriendShipIdGuid {get;set;}
public Guid UserIdGuid {get;set;}
public Guid FriendIdGuid {get;set;}
public User User {get;set;}
public User Friend {get;set;}
}
class User
{
public Guid IdGuid {get;set;}
public string Name {get;set;}
// Only if you will allow duplicates in UserFriendship
public virtual List<UserFriendship> Friendships {get;set;}
}
I don't remember the exact syntax/methods of Fluent API but you will get the idea:
modelBuilder.Entity<User>()
.HasKey(u => u.IdGuid)
.ToTable("User");
modelBuilder.Entity<UserFriendship>()
.HasKey(uf => uf.FriendShipIdGuid)
.ToTable("UserFriendship");
modelBuilder.Entity<UserFriendship>()
.HasRequired(uf => uf.User)
.WithMany(u => u.Friendships) // Only if you allow duplicates
//.WithMany() // Otherwise
.HasForeignKey(uf => uf.UserIdGuid);
modelBuilder.Entity<UserFriendShip>()
.HasRequired(uf => uf.Friend)
.WithMany()
.HasForeignKey(uf => uf.FriendIdGuid);