I wanted to get this done without having to write fluent API code. So here is my take.
The following example trying to save whomever other users profiles the user visited and who visited that user profile. Sadly, the following example doesn't support extra properties other than two ids of the visiting and the visited user.
The navigation properties had been linked using the InversePropertyAttribute.
see more about it in entityframework.net and entityframeworktutorial.net
Model ↴
public class User
{
[InverseProperty(nameof(User.VisitingUsers))]
public virtual List<User> VisitedUsers { get; set; }
[NotMapped]
public long? VisitedUsersCount { get { return this.VisitedUsers == null ? 0 : this.VisitedUsers.Count(); } }
[InverseProperty(nameof(User.VisitedUsers))]
public virtual List<User> VisitingUsers { get; set; }
[NotMapped]
public long? VisitingUsersCount { get { return this.VisitingUsers == null ? 0 : this.VisitingUsers.Count(); } }
}
Generated Migration Code ↴
CreateTable(
"dbo.UserUsers",
c => new
{
User_Id = c.Long(nullable: false),
User_Id1 = c.Long(nullable: false),
})
.PrimaryKey(t => new { t.User_Id, t.User_Id1 })
.ForeignKey("dbo.Users", t => t.User_Id)
.ForeignKey("dbo.Users", t => t.User_Id1)
.Index(t => t.User_Id)
.Index(t => t.User_Id1);