Role in user is empty

丶灬走出姿态 提交于 2019-12-24 07:13:33

问题


When I do _userManager.FindByNameAsync(email) I am able to retrieve the correct user record. However, my roles field has a count of 0. If I look in the db, there is in fact roles associated to my user's id.

I am on .NET 2.0

In my startup.cs:

services.AddIdentity<ApplicationUser, ApplicationRole>

My applicationRole :

public class ApplicationRole : IdentityRole
{
    // public virtual ICollection<ApplicationUser> Users { get; } = new List<ApplicationUser>();

    public ApplicationRole(){}
    public ApplicationRole(string roleName) : base(roleName) {}
    public virtual ICollection<IdentityUserRole<string>> Users { get; set; }

    public virtual ICollection<IdentityRoleClaim<string>> Claims { get; set; }
}

My applicationUser:

public class ApplicationUser : IdentityUser
{
    [InverseProperty("User")]
    public virtual Player Player { get; set; }

    public List<ApplicationUserClubs> Clubs { get; set; }
    public int? CustomerProfileId { get; set; }
    /// <summary>
    /// Navigation property for the roles this user belongs to.
    /// </summary>
    public virtual ICollection<IdentityUserRole<string>> Roles { get; } = new List<IdentityUserRole<string>>();
}

My query is:

ApplicationUser user = await _userManager.FindByNameAsync(model.Email);

This used to work fine before my migration over to .NET 2.0

EDIT:

foreach (IdentityRole role in allRoles)
{
    bool isUserInRole = role.Users.FirstOrDefault(x => x.Id == u.Id) != null;
    userInRoles.Add(item: new AdminUserRoleViewModel
    {
        Id = role.Id,
        Name = role.Name,
        IsUserInRole = isUserInRole
    });
}

I get an exception when I try to do Role.Users. The exception says:

'IdentityRole' does not contain a definition for 'Users' and no extension method 'Users' accepting a first argument of type 'IdentityRole' could be found (are you missing a using directive or an assembly reference?

That's when I decided to extend IdentityRole with my custom class ApplicationRole

Also here is my context File:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<IdentityUser>().ToTable("AspNetUsers")
        .HasKey(x => x.Id);

    builder.Entity<IdentityRole>().ToTable("AspNetRoles")
        .HasKey(x => x.Id);

    builder.Entity<IdentityUserRole<string>>().ToTable("AspNetUserRoles")
        .HasKey(x => new { x.UserId, x.RoleId });

    builder.Entity<IdentityUserClaim<string>>().ToTable("AspNetUserClaims")
        .HasKey(x => x.Id);

    builder.Entity<IdentityUserLogin<string>>().ToTable("AspNetUserLogins")
        .HasKey(x => new { x.UserId, x.ProviderKey });

    builder.Entity<IdentityUserToken<string>>().ToTable("AspNetUserTokens")
        .HasKey(x => new { x.UserId, x.LoginProvider, x.Name });

    builder.Entity<IdentityRoleClaim<string>>().ToTable("AspNetRoleClaims")
        .HasKey(x => x.Id);

    builder.Entity<ApplicationUserToken>().ToTable("UserTokens")
    .HasKey(x => x.Id);
}

来源:https://stackoverflow.com/questions/49786671/role-in-user-is-empty

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