Entity Framework 核心加入身份 AspNetUser table。自定义的 AspNetUser Id tables/Entities
Entity Framework Core Join identity AspNetUser table. AspNetUser Id to custom tables/Entities
我启动了一个新的 Blazor 应用程序,我正在尝试使用 Entity FrameworkCore。我想 link Identity AspNetUsers table。我想要与 UserAddress Table 的一对多关系。一个 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 之间的一对多关系 table
您可以像这样创建 one-to-many 关系。
用户地址class:
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; }
}
然后开始迁移并更新数据库。
结果:
我启动了一个新的 Blazor 应用程序,我正在尝试使用 Entity FrameworkCore。我想 link Identity AspNetUsers table。我想要与 UserAddress Table 的一对多关系。一个 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 之间的一对多关系 table
您可以像这样创建 one-to-many 关系。
用户地址class:
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; }
}
然后开始迁移并更新数据库。
结果: