我正在尝试使用 EFCore 在同一用户类上创建多对多关系,该用户类基于 EntityFrameworkCore.IdentityUser,遵循描述的说明 here , here和 here 。我有以下用户类别:
public class MyApplicationUser : IdentityUser
{
public virtual ICollection<MyApplicationUserJunction> MyApplicationUsers { get; set; }
public virtual ICollection<MyApplicationUserJunction> ManagingMyApplicationUsers { get; set; }
}
这是我的连接表:
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; }
}
这是我的 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);
}
每当我运行 EFCore 工具添加迁移命令时,都会收到以下错误:
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.
我正在使用 EntityFrameworkCore.Tools.DotNet 包 1.0.0-preview3-final。
我尝试仅在 User 类上使用单个导航属性,并且尝试不指定 WithMany 函数,如here所述。但没有成功。
最佳答案
为了使其与 EF Core 1 一起工作,按照上面 Rufo 爵士的建议,我指定了 TKey 的类型:
public class MyApplicationUser : IdentityUser<int>
{
public virtual ICollection<MyApplicationUserJunction> MyApplicationUsers { get; set; }
public virtual ICollection<MyApplicationUserJunction> ManagingMyApplicationUsers { get; set; }
}
在上下文中:
public class MyApplicationContext : IdentityDbContext<MyApplicationUser, IdentityRole<int>, int>
{
...
在ConfigureServices 的AddIdentity 部分中,它也使用通用IdentityRole:
services.AddIdentity<MyApplicationUser, IdentityRole<int>>(config =>
{
config.User.RequireUniqueEmail = true;
config.Password.RequiredLength = 8;
config.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
})
.AddEntityFrameworkStores<MyApplicationContext, int>();
然后,我将其升级到 EF Core 2,这需要进行一些其他更改才能使身份正常工作。正如pealmeid here所述您必须从 IdentityRole 派生:
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>();
...
我之前也使用过 cookie 身份验证,该身份验证不再在 AddIdentity 配置 lambda 中配置,而是在服务级别完成:
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureApplicationCookie(options => options.LoginPath = "/Auth/Login");
...
还对 DbContext 签名进行了轻微更改以使用派生角色类型:
public class MyApplicationContext : IdentityDbContext<MyApplicationUser, MyRole, int>
{
关于c# - 同一类上的多对多关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46145679/