User Role/Authorization doesn't work in ASP.NET Identity

不羁的心 提交于 2019-12-07 00:28:28
Joneel Lasaga

The code below will fix IdentityUserRoles table Foreign Keys issue.

var user = modelBuilder.Entity<TUser>()
    .ToTable("AspNetUsers");
user.HasMany(u => u.Roles).WithRequired().HasForeignKey(ur => ur.UserId);
user.HasMany(u => u.Claims).WithRequired().HasForeignKey(uc => uc.UserId);
user.HasMany(u => u.Logins).WithRequired().HasForeignKey(ul => ul.UserId);
user.Property(u => u.UserName).IsRequired();

modelBuilder.Entity<TUserRole>()
    .HasKey(r => new { r.UserId, r.RoleId })
    .ToTable("AspNetUserRoles");

modelBuilder.Entity<TUserLogin>()
    .HasKey(l => new { l.UserId, l.LoginProvider, l.ProviderKey})
    .ToTable("AspNetUserLogins");

modelBuilder.Entity<TUserClaim>()
    .ToTable("AspNetUserClaims");

var role = modelBuilder.Entity<TRole>()
    .ToTable("AspNetRoles");
role.Property(r => r.Name).IsRequired();
role.HasMany(r => r.Users).WithRequired().HasForeignKey(ur => ur.RoleId);

I found my answer here. Create ASP.NET Identity tables using SQL script!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!