经过一番明智的搜索,我找到了这个Authorize and GetRoles doesn't work in ASP.NET Identity 帖子。这与对迁移的一些编辑相结合解决了这个问题。
总结:
我加了
base.OnModelCreating(modelBuilder);
到我的上下文文件
并编辑了模型生成部分
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
modelBuilder.Entity<IdentityRole>().ToTable("Role");
modelBuilder.Entity<ApplicationUser>().ToTable("User");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");
//modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
//modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
//modelBuilder.Entity<ApplicationUser>().HasMany<IdentityUserRole>(u => u.Roles);
//modelBuilder.Entity<IdentityUserRole>().HasKey(r => new
//{
// r.RoleId,
// r.UserId
//});
您的迁移代码尝试删除某些列并重命名其他列,因此我没有删除和重命名,而是删除了要重命名的列并保留了预先存在的列。要删除预先存在的列,您还必须删除外键。我还必须阻止它重新创建主键。
DropIndex("dbo.UserClaim", new[] { "ApplicationUser_Id" });
DropIndex("dbo.UserLogin", new[] { "ApplicationUser_Id" });
DropIndex("dbo.UserRole", new[] { "ApplicationUser_Id" });
DropIndex("dbo.UserRole", new[] { "IdentityRole_Id" });
DropForeignKey("FK_dbo_UserClaim_ApplicationUser_Id", "ApplicationUser_Id");
DropForeignKey("FK_dbo_UserLogin_ApplicationUser_Id", "ApplicationUser_Id");
DropForeignKey("FK_dbo_UserRole_ApplicationUser_Id", "ApplicationUser_Id");
DropForeignKey("FK_dbo_UserRole_IdentityRole_Id", "Identity_Id");
DropColumn("dbo.UserClaim", "ApplicationUser_Id");
DropColumn("dbo.UserLogin", "ApplicationUser_Id");
DropColumn("dbo.UserRole", "ApplicationUser_Id");
DropColumn("dbo.UserRole", "IdentityRole_Id");
//DropPrimaryKey("dbo.UserLogin");
//DropPrimaryKey("dbo.UserRole");
AlterColumn("dbo.User", "Email", c => c.String(maxLength: 256));
AlterColumn("dbo.User", "UserName", c => c.String(nullable: false, maxLength: 256));
AlterColumn("dbo.UserClaim", "UserId", c => c.String(nullable: false, maxLength: 128));
AlterColumn("dbo.UserClaim", "UserId", c => c.String(nullable: false, maxLength: 128));
AlterColumn("dbo.UserLogin", "LoginProvider", c => c.String(nullable: false, maxLength: 128));
AlterColumn("dbo.UserLogin", "ProviderKey", c => c.String(nullable: false, maxLength: 128));
AlterColumn("dbo.UserLogin", "UserId", c => c.String(nullable: false, maxLength: 128));
AlterColumn("dbo.UserRole", "UserId", c => c.String(nullable: false, maxLength: 128));
AlterColumn("dbo.UserRole", "RoleId", c => c.String(nullable: false, maxLength: 128));
AlterColumn("dbo.Role", "Name", c => c.String(nullable: false, maxLength: 256));
//AddPrimaryKey("dbo.UserLogin", new[] { "LoginProvider", "ProviderKey", "UserId" });
//AddPrimaryKey("dbo.UserRole", new[] { "UserId", "RoleId" });
CreateIndex("dbo.User", "UserName", unique: true, name: "UserNameIndex");
CreateIndex("dbo.UserClaim", "UserId");
CreateIndex("dbo.UserLogin", "UserId");
CreateIndex("dbo.UserRole", "UserId");
CreateIndex("dbo.UserRole", "RoleId");
CreateIndex("dbo.Role", "Name", unique: true, name: "RoleNameIndex");
这是一次令人沮丧的 Microsoft ASP.NEt Identity 体验,我希望以上内容可以帮助人们多留几缕头发。