EF Core通过aspnetUser表向IdentityDBContext中添加多个实体



我有一个IdentityDBContext,我想通过EF Core加入到其他实体。

目前我正在使用项目创建时的默认身份验证。

我正试图找出如何从aspnetUsers表加入id到多个自定义表。

下面的URL只解释了如何将它添加到一个表,而不是多个表。

Entity Framework Core Join identity AspNetUser表。自定义表/实体的AspNetUser Id

的例子:

public class Inventory
{
public int ID { get; set; }
public string ItemName { get; set; }
public int amountQuantity { get; set; }
public DateTime UpdateDate { get; set; }
-- userid from aspnetuserid
}
public class TestModel
{
public int id { get; set; }
public string mysex { get; set; }
-- userid from aspnetuserid
}
public class WorkOutItems
{
public int Id { get; set; }
public string ItemName { get; set; }
public int CountOfItemName { get; set; }
-- userid from aspnetuserid
}
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Inventory> Inventory { get; set; } -- this to have aspnetuserid
public DbSet<TestModel> TestModels { get; set; }-- this to have aspnetuserid
public DbSet<WorkOutItems> WorkOutItems { get; set; }-- this to have aspnetuserid
}

结果是这样的

public class ApplicationUser : IdentityUser
{
public ICollection<TestModel> UserAddresses { get; set; }
public ICollection<Inventory> Inventory { get; set; }
public ICollection<WorkOutItems> WorkOutItems { get; set; }
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(b => { b.HasMany(p => p.UserAddresses); });
builder.Entity<ApplicationUser>(b => { b.HasMany(p => p.Inventory); });
builder.Entity<ApplicationUser>(b => { b.HasMany(p => p.WorkOutItems); });
}

最新更新