我在加载我在.NET核心MVC应用程序中分配给ApplicationUser的实体时遇到问题。
我已将我的一个实体添加到用户类,请参阅以下代码:
public class ApplicationUser : IdentityUser
{
public int? AzureBlobResourceId { get; set; }
[ForeignKey("AzureBlobResourceId")]
public AzureBlobResource AzureBlobResource { get; set; }
}
理想情况下,我希望在从UserManager检索用户时加载AzureBlobResource对象
private Task<ApplicationUser> GetCurrentUserAsync()
{
return _userManager.GetUserAsync(HttpContext.User);
}
不幸的是,AzureBlobResource对象始终为null,即使AzureBlobResourceId具有值。
我在这里缺少什么?
谢谢,尼古拉
答案 0 :(得分:0)
您需要实施您的用户界面
public class ApplicationUser : IdentityUser {
public int? AzureBlobResourceId { get; set; }
[ForeignKey("AzureBlobResourceId")]
public AzureBlobResource AzureBlobResource { get; set; }
}
public class MyAppUserStore : UserStore<ApplicationUser>
{
public MyAppUserStore(DbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
{
}
public override async Task<ApplicationUser> FindByIdAsync(string userId, CancellationToken cancellationToken = new CancellationToken())
{
return await Context.Set<ApplicationUser>().Include(p => p.AzureBlobResource).FirstOrDefaultAsync(u => u.Id == userId, cancellationToken: cancellationToken);
}
}
在Sturtup.cs中添加
ervices.AddIdentity<ApplicationUser, IdentityRole>()
.AddUserStore<MyAppUserStore >()
.AddUserManager<UserManager<ApplicationUser>>()
.AddDefaultTokenProviders();