选项 1。
添加了这个并且它起作用了:
builder.Entity<IdentityUserRole<Guid>>().HasKey(p => new { p.UserId, p.RoleId });
选项 2。
最常见的原因
模型名与数据库中的表名不匹配
EntityFramework 无法按照约定找出所需的元数据,并且您没有覆盖它。
AppDbContext 不是从 DbContext 继承的,而是应该从 IdentityDbContext 继承
在 accountcontroller.cs 和 startup.cs 模型不匹配然后这种类型的问题已经发生例如
public class Role : IdentityRole
{
}
在您的 DbContext.cs 中
public class DataContext : IdentityDbContext<ApplicationUser, Role, string, IdentityUserClaim<string>, IdentityUserRole<string>,
IdentityUserLogin<string>, IdentityRoleClaim<string>, IdentityUserToken<string>>
{
public DbSet<Role> Role { get; set; }
public DataContext(DbContextOptions<DataContext> options)
: base(options)
{
}
}
在你的 startup.cs 中
services.AddIdentity<IdentityUser, IdentityRole>();
在上面给出的,有一个不同的角色模型,所以这不起作用。你可以改startup.cs打电话
替换
services.AddIdentity<IdentityUser, IdentityRole>()
到
services.AddIdentity<IdentityUser, Role>()
因为您在 DataContext 中添加了 Role 类
选项 3。
检查您的AppDbContext 不是继承自DbContext,而是应该继承自IdentityDbContext<ApplicationUser>