I'm trying to implement what seems like a pretty common case - a Friendship relationship between 2 users. I think the models are self-explanatory. Friendship
needs to have 2 users on it and also some data about the relationship, and users have a Friendships
property, which is populated by any friendship they are a part of, as either User1
or User2
. I couldn't figure out a nicer way to name the 2 users. Here are my models:
public class Friendship : Entity
{
public ApplicationUser User1 { get; set; }
public ApplicationUser User2 { get; set; }
...
}
public class ApplicationUser : IdentityUser
{
public virtual List<Friendship> Friendships { get; set; }
...
}
Here's my attempt at configuring the relationship in OnModelCreating
:
modelBuilder.Entity<ApplicationUser>()
.HasMany(x => x.Friendships)
.WithRequired()
.Map(t => t.MapKey("User1_Id", "User2_Id"));
I don't think I'm doing the configuration right. Here's the error I'm getting when trying to create a migration from this:
The specified association foreign key columns 'User1_Id, User2_Id' are invalid. The number of columns specified must match the number of primary key columns.
Is it possible to accomplish this using ef6? A special thanks to anyone that can help.