C# 查询EF核心身份角色

C# 查询EF核心身份角色,c#,asp.net,entity-framework,asp.net-core,entity-framework-core,C#,Asp.net,Entity Framework,Asp.net Core,Entity Framework Core,我正在使用.NETCore3.1,我想查询数据库中的用户 因此,我修改了我的ApplicationUser类,如下所示: public class ApplicationUser : IdentityUser { public ICollection<IdentityUserRole<string>> UserRoles { get; set; } } public class ApplicationUser : IdentityUser { publi

我正在使用.NETCore3.1,我想查询数据库中的用户

因此,我修改了我的
ApplicationUser
类,如下所示:

public class ApplicationUser : IdentityUser
{
    public ICollection<IdentityUserRole<string>> UserRoles { get; set; }
}
public class ApplicationUser : IdentityUser
{
    public ICollection<IdentityRole> Roles { get; set; }
}
问题是
UserRoles
具有
RoleId
属性,但它们没有
Role
属性

那么,我如何获得这些角色的名称呢

更新:

根据Jawad的评论,我将代码更改如下:

public class ApplicationUser : IdentityUser
{
    public ICollection<IdentityUserRole<string>> UserRoles { get; set; }
}
public class ApplicationUser : IdentityUser
{
    public ICollection<IdentityRole> Roles { get; set; }
}
我想我不能这么做,因为这是一种多对多的关系。但事实上,它确实可以编译。然而,当我运行它时,我得到一个错误

列名“ApplicationSerID”无效

我不确定该列名引用的位置。它似乎不是数据库中的列名

更新2:

事实证明,上面的错误是因为数据库迁移挂起。但是,我不想更改数据库。正如我最初怀疑的那样,我无法执行
ApplicationUser.ICollection
,因为有一个中间表

所以我让它运行,但它没有返回结果(因为从
IdentityRole
ApplicationUser
没有外键)


因此,问题是:如何查询用户及其角色?

您应该创建一个名为
ApplicationUserRole
的新类,该类继承自
IdentityUserRole
,然后您只需要定义
ApplicationUser
->
ApplicationUserRole
Appli之间的关系cationRole
->
ApplicationUserRole

modelBuilder.Entity<ApplicationUserRole>(entity =>
{
    entity
      .HasOne(x => x.Role)
      .WithMany(x => x.UserRoles)
      .HasForeignKey(x => x.RoleId);

    entity
      .HasOne(x => x.User)
      .WithMany(x => x.UserRoles)
      .HasForeignKey(x => x.UserId);
 });
modelBuilder.Entity(Entity=>
{
实体
.HasOne(x=>x.Role)
.WithMany(x=>x.UserRoles)
.HasForeignKey(x=>x.RoleId);
实体
.HasOne(x=>x.User)
.WithMany(x=>x.UserRoles)
.HasForeignKey(x=>x.UserId);
});
您还应该用应用程序类替换对标识类的任何引用

public class ApplicationUserRole : IdentityUserRole<string>
{
    public ApplicationUser User { get; set; }

    public ApplicationRole Role { get; set; }
}

public class ApplicationUser : IdentityUser<string>
{
    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

public class ApplicationRole : IdentityRole<string>
{
    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}
公共类ApplicationUserRole:IdentityUserRole
{
公共应用程序用户{get;set;}
公共应用程序角色{get;set;}
}
公共类应用程序用户:IdentityUser
{
公共ICollection用户角色{get;set;}
}
公共类应用程序角色:IdentityRole
{
公共ICollection用户角色{get;set;}
}

因此,我不知道为什么
IdentityUserRole
不包含
IdentityRole
属性。这似乎是一个不必要的限制

最后,我只是更改了我的查询

var users = await (from u in DbContext.Users
                   join ur in DbContext.UserRoles on u.Id equals ur.UserId
                   join r in DbContext.Roles on ur.RoleId equals r.Id
                   select new
                   {
                       u.Email,
                       u.EmailConfirmed,
                       u.PhoneNumber,
                       Role = r.Name
                   })
                   .ToListAsync();

Users = (from u in users
         group u.Role by u into g
         select new UserViewModel
         {
             Email = g.Key.Email,
             EmailConfirmed = g.Key.EmailConfirmed,
             Phone = g.Key.PhoneNumber,
             Roles = string.Join(", ", g)
         })
         .ToList();
我无法使
groupby
在查询中工作,因此我将在检索数据后对其进行分组


无论如何,它是有效的。

你能不能将
角色
属性添加到
用户角色
?@cantSleepNow:不知道你的意思。正如你所看到的,
用户角色
是类型
ICollection
,而
IdentityUserRole
是在.NET核心库中定义的。我不能编辑它。是的,现在看到了,没有立即意识到它是一个net核心类你确定你不是在寻找
IdentityRoles
而不是
IdentityUserRoles
IdentityUserRoles
只有
RoleId
UserId
作为成员,其中
IdentityRoles
有一个名称字段。通过
IdentityRoles
,你可以使用
u.UserRoles.Select(r=>r.Name)
没有这个角色。@Jawad:嗯,这是一种多对多关系,所以我不确定你是否能做到。无论如何,
IdentityRoles
没有为我定义。你使用的是什么版本?@JonathanWood它不是重复的,它将
ApplicationUserRole
链接到
ApplicationUser
ApplicationUserRole
链接到
ApplicationRole
就像我说的,我已经让它工作了,为一个查询添加其他东西对我来说是不值得的。但是如果没有实际测试它,这看起来是正确的答案。我仍然认为
IdentityUserRole
应该有一个
IdentityRole
属性,但我把它标记为正确的答案(在我的上面)。