C# 同一类上的多对多关系不起作用

C# 同一类上的多对多关系不起作用,c#,many-to-many,entity-framework-core,C#,Many To Many,Entity Framework Core,我正在尝试在同一个用户类上使用EFCore创建多对多关系,该用户类基于EntityFrameworkCore.IdentityUser,并遵循所述说明。我有以下用户类: public class MyApplicationUser : IdentityUser { public virtual ICollection<MyApplicationUserJunction> MyApplicationUsers { get; set; } public virtual I

我正在尝试在同一个用户类上使用EFCore创建多对多关系,该用户类基于EntityFrameworkCore.IdentityUser,并遵循所述说明。我有以下用户类:

public class MyApplicationUser : IdentityUser
{
    public virtual ICollection<MyApplicationUserJunction> MyApplicationUsers { get; set; }
    public virtual ICollection<MyApplicationUserJunction> ManagingMyApplicationUsers { get; set; }
}
以下是我的onmodel配置:

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);
}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
builder.Entity()
.HasKey(uj=>new{uj.managedmyaapplicationserid,uj.myapplicationserid});
builder.Entity()
.HasOne(uj=>uj.MyApplicationUser)
.WithMany(qu=>qu.MyApplicationUsers)
.HasForeignKey(uj=>uj.myapplicationserid).OnDelete(DeleteBehavior.Restrict);
builder.Entity()
.HasOne(uj=>uj.ManagedMyApplicationUser)
.WithMany(qu=>qu.ManagingMyApplicationUsers)
.HasForeignKey(uj=>uj.managedMyApplication系列);
}
每当运行EFCore tools add migrations命令时,都会出现以下错误:

具有外键属性{'MyApplicationUserId':int}的从'MyApplicationUserJunction.MyApplicationUser'到'MyApplicationUser.MyApplicationUsers'的关系不能以主键{'Id':string}为目标,因为它不兼容。为此关系配置一个主键或一组兼容的外键属性

我正在使用EntityFrameworkCore.Tools.DotNet包1.0.0-preview3-final


我曾尝试在用户类上仅使用一个导航属性,并尝试不按所述指定WithMany函数,但没有成功。

为了使用EF Core 1实现这一点,根据上面Rufo爵士的建议,我指定了TKey的类型:

public class MyApplicationUser : IdentityUser<int>
{
    public virtual ICollection<MyApplicationUserJunction> MyApplicationUsers { get; set; }
    public virtual ICollection<MyApplicationUserJunction> ManagingMyApplicationUsers { get; set; }
}
另外,对DbContext签名稍作更改,以使用派生角色类型:

public class MyApplicationContext : IdentityDbContext<MyApplicationUser, MyRole, int>
{
公共类MyApplicationContext:IdentityDbContext {
阅读文档并查看类型。外键属性必须是相同的类型谢谢!我将查看在UserIdentity中使用int作为主键的情况。您是否已成功使用它?如果是,请回答问题以分享知识。抱歉@MohammedNoureldin我确实使用了它,我将在今天晚些时候发布答案@Mohammed Noureldin回答说,如果我错过了什么,请告诉我,我已经有一段时间没有做这些更改了谢谢!事实上,这就是我一直在等待看到的,第二个
ICollection
属性。我不确定这是否是正确的方式。
public class MyApplicationContext : IdentityDbContext<MyApplicationUser, IdentityRole<int>, int>
{
    ...
services.AddIdentity<MyApplicationUser, IdentityRole<int>>(config =>
        {
            config.User.RequireUniqueEmail = true;
            config.Password.RequiredLength = 8;
            config.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
        })
        .AddEntityFrameworkStores<MyApplicationContext, int>();
public class MyRole : IdentityRole<int>
{
    public MyRole() : base()
    {
    }

    public MyRole(string roleName)
    {
        Name = roleName;
    }
}
public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentity<MyApplicationUser, MyRole>(config =>
            {
                config.User.RequireUniqueEmail = true;
                config.Password.RequiredLength = 8;
            }).AddEntityFrameworkStores<MyApplicationContext>();
            ...
public void ConfigureServices(IServiceCollection services)
    {
        services.ConfigureApplicationCookie(options => options.LoginPath = "/Auth/Login");
        ...
public class MyApplicationContext : IdentityDbContext<MyApplicationUser, MyRole, int>
{