C# 查询EF核心身份角色
我正在使用.NETCore3.1,我想查询数据库中的用户 因此,我修改了我的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
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
属性,但我把它标记为正确的答案(在我的上面)。