asp.net - Entity Framework Core Join 标识 AspNetUser 表。自定义表/实体的 AspNetUser Id
问题描述
我启动了一个新的 Blazor 应用程序,并尝试使用 Entity FrameworkCore。我想链接 Identity AspNetUsers 表。我想要一个与 UserAddress 表的一对多关系。一个 AspNetUser 有多个地址。
public class UserAddress
{
public string Id { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
' I don't know what to put here to connect this table with the AspNetUsers Table
public int UserId {get;set} This doesn't work
}
我不知道该怎么做才能让 EF 构建 AspNetUsers Id 和 UserAddress 表之间的一对多关系
解决方案
您可以像这样创建一对多的关系。
用户地址类:
public class UserAddress
{
public string Id { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
}
新的 ApplicationUser 继承 Identityuser:
public class ApplicationUser:IdentityUser
{
public ICollection<UserAddress> UserAddresses { get; set; }
}
在 ApplicationDbContext 中进行如下更改:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(b => { b.HasMany(p => p.UserAddresses); }) ;
}
public DbSet<UserAddress> UserAddresses { get; set; }
}
然后开始迁移并更新数据库。
推荐阅读
- ios - Xcode 模拟器正确显示纹理但不显示设备
- python - docvecs.most_similar() 无法正常工作。找不到文档
- sql-server - 通过链接服务器到 Oracle 源使用 openquery 时结果不一致
- python - 小部件形状和小部件影响检查
- javascript - 无法测试 redux 异步操作
- regex - 我能做些什么来防止某些字符在正则表达式中匹配?
- c# - 将特定列的输入解析为单独的列的最有效方法是什么?
- python-3.x - “SumExpression”对象不可迭代
- javascript - Javascript:将 JSON 对象数组结构化为脚本的问题
- google-apps-script - 导入后是否可以使 Google 表格导入范围保持静态?