I am trying to create a many to many relationship using EFCore on the same user class which I have based on EntityFrameworkCore.IdentityUser following the instructions described here, here and here. I have the following user class:
public class MyApplicationUser : IdentityUser
{
public virtual ICollection<MyApplicationUserJunction> MyApplicationUsers { get; set; }
public virtual ICollection<MyApplicationUserJunction> ManagingMyApplicationUsers { get; set; }
}
Here is my joining table:
public class MyApplicationUserJunction
{
public int MyApplicationUserId { get; set; }
public virtual MyApplicationUser MyApplicationUser { get; set; }
public int ManagedMyApplicationUserId { get; set; }
public virtual MyApplicationUser ManagedMyApplicationUser { get; set; }
}
Here is my OnModelConfiguring:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<MyApplicationUserJunction>()
.HasKey(uj => new { uj.ManagedMyApplicationUserId, uj.MyApplicationUserId});
builder.Entity<MyApplicationUserJunction>()
.HasOne(uj => uj.MyApplicationUser)
.WithMany(qu => qu.MyApplicationUsers)
.HasForeignKey(uj => uj.MyApplicationUserId).OnDelete(DeleteBehavior.Restrict);
builder.Entity<MyApplicationUserJunction>()
.HasOne(uj => uj.ManagedMyApplicationUser)
.WithMany(qu => qu.ManagingMyApplicationUsers)
.HasForeignKey(uj => uj.ManagedMyApplicationUserId);
}
Whenever I run the EFCore tools add migrations command, I get the following error:
The relationship from 'MyApplicationUserJunction.MyApplicationUser' to 'MyApplicationUser.MyApplicationUsers' with foreign key properties {'MyApplicationUserId' : int} cannot target the primary key {'Id' : string} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.
I am using the EntityFrameworkCore.Tools.DotNet package 1.0.0-preview3-final.
I have tried only using a single navigation property on the User class, and I've tried not specifying the WithMany function as described here but with no success.