c# - 无法在核心身份的 ApplicationUser 中保留关系
问题描述
我找不到正确扩展 ASP.NET Core 的方法IdentityUser
(扩展后:)ApplicationUser
。我可以覆盖它并将其与其他模型链接,但不能从用户链接到其他模型。CustomTag
像作品一样的简单数据。
问题的源代码:https
://github.com/chanibal/CoreIdentityIssue
或者,具体来说,它是第二次提交,其中包含对默认模板的所有更改
我做了什么:
- 创建新项目:
ASP.NET Core Web 应用程序(模型-视图-控制器)
身份验证:个人用户帐户,在应用程序中存储用户帐户
更新了数据库 更改了覆盖的模型
IdentityUser
(如官方文档中所示):public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {} public DbSet<Bar> Bars { get; set; } public DbSet<Foo> Foos { get; set; } public DbSet<ApplicationUser> ApplicationUsers { get; set; } //< doesn't help } public class ApplicationUser : IdentityUser { public string CustomTag { get; set; } //< this works public Bar Bar { get; set; } //< THIS DOES NOT WORK } public class Bar { public int Id { get; set; } public int Value { get; set; } } public class Foo { public int Id { get; set; } public ApplicationUser User { get; set; } //< this works }
并迁移了更改
我正在以这种方式更新 Bar 值:
/// This should ensure a Bar is connected to a ApplicationUser
/// and increment it's value
[Authorize]
public async Task<IActionResult> IncrementBar()
{
// This DOES NOT work
var user = await _userManager.GetUserAsync(HttpContext.User);
if (user.Bar == null) //< this is always null
{
user.Bar = new Bar() { Value = 0 };
// _context.Add(user.Bar); //< doesn't help
}
user.Bar.Value++;
// await _userManager.UpdateAsync(user); //<> doesn't help
// await _signInManager.RefreshSignInAsync(user); //< doesn't help, starting to get desperate
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
信息在数据库中,可通过 SQL 访问。但不知何故,它不会水合ApplicationUser
模型:
使用 Visual Studio 16.3.9
解决方案
EF 不会自动加载相关实体。您必须急切地或显式地加载关系。急切加载是首选方式,因为它会在单个查询中获取所有数据。但是,UserManager<TUser>
无法提供急切加载关系的方法。因此,您有两个选择:
显式加载关系。不过,这将需要一个额外的查询。
var user = await _userManager.GetUserAsync(HttpContext.User); await _context.Entry(user).Reference(x => x.Bar).LoadAsync(); // note: for collection props, you'd use `Collection(x => x.CollectionProp).LoadAsync()` instead.
通过用户 id 从上下文中查询用户,而不是使用
UserManager<TUser>
:var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var user = await _context.Users.Include(x => x.Bar).SingleOrDefaultAsync(x => x.Id == userId);
推荐阅读
- kubernetes - Kubernetes 浪费资源
- api - npm request - 用于 twitch 数据的自定义 HTTP 标头
- spring - 获取 java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder 更新 Spring Cloud 版本到 Brixton.SR6 后
- javascript - ReactJS - 如何将字符串中的html元素列表作为html元素返回
- elasticsearch - 将多类型索引迁移到单类型索引
- node-red - 无法使用 Switch 节点可视化我的数据
- android - Android - Kotlin:在扩展应用程序的类中实例化一个对象并在所有活动中使用它
- javascript - vue组件上的js函数
- javascript - SAPUI5 表在获取选定行数据时出现多选错误
- sql - 是否有可能将单元格数据拆分为 Oracle 中的多个列