Ciao
我正在使用IdentityServer和ASP.NET Identity从事.NET Core 2.2项目。
我有以下实体:
ExtraDatas表
+----+-------------+
| Id | Description |
+----+-------------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+-------------+
AspNetRoles
+----+-------+----------------+------------------+
| Id | Name | NormalizedName | ConcurrencyStamp |
+----+-------+----------------+------------------+
| 1 | Admin | Admin | [NULL] |
| 2 | User | User | [NULL] |
+----+-------+----------------+------------------+
RoleExtraDatas
+-------------+-----------------+
| RoleId (FK) | ExtraDataId (FK)|
+-------------+-----------------+
| 1 | 1 |
| 1 | 3 |
| 2 | 2 |
+-------------+-----------------+
由以下类代表:
public class ExtraData
{
[Key, Required]
public int Id { get; set; }
[Required, MaxLength(128)]
public string Description { get; set; }
public virtual ICollection<RoleExtraData> ExtraDatas { get; set; }
}
public class ApplicationRole: IdentityRole
{
public virtual ICollection<RoleExtraData> ExtraDatas { get; set; }
}
public class RoleExtraData
{
public string RoleId { get; set; }
public ApplicationRole Role { get; set; }
public int ExtraDataId { get; set; }
public ExtraData ExtraDatas { get; set; }
}
在我的 Startup.cs 中,我已经配置了ASP.NET Identity,并传递了我的 ApplicationRole 类的引用:
services.AddIdentity<IdentityUser, ApplicationRole>()
.AddEntityFrameworkStores<MyIdentityDbContext>()
.AddDefaultTokenProviders();
在具有依赖项注入的服务内部,我获得了 UserManager 和 RoleManager 属性,但是当我尝试执行此代码时, role.ExtraDatas 是空:
IdentityUser user = userManager.GetUserAsync(context.Subject).Result;
Task<IList<string>> roleList = userManager.GetRolesAsync(user);
foreach (var roleId in roleList.Result)
{
ApplicationRole role = roleManager.FindByNameAsync(roleId).Result;
// the property role.ExtraDatas is empty
}
如何解决此问题?
非常感谢