实体框架核心加入身份AspNetUser表。 AspNetUser ID到自定义表/实体

时间:2020-09-29 04:44:46

标签: asp.net entity-framework asp.net-core entity-framework-core asp.net-identity

我启动了一个新的Blazor应用程序,并且尝试使用Entity FrameworkCore。我想链接身份AspNetUsers表。我想要与UserAddress表建立1对多关系。一个AspNetUser有很多地址。

wind_chill<-function(df){
  twc<- (35.74)+(0.6215)*(df$HighTemp) - (35.75)* (df$WindSpeed)^0.16+
        (0.4275)*(df$HighTemp*df$WindSpeed)^0.16
  return(twc)
}

result <- wind_chill(jan_weather)

我不知道如何让EF在AspNetUsers ID和UserAddress表之间构造一对多关系

1 个答案:

答案 0 :(得分:3)

您可以像这样创建一对多关系。

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; }
}

然后开始迁移并更新数据库。

结果: enter image description here