0

I have a IdentityDBContext which i would like to join to other entities via EF Core.

Currently i am using the default authentication from the project creation.

I am trying to figure out how i would join the id from the aspnetUsers table into multiple custom tables.

Below URL only explains how to add it to one table, but not many tables.

Entity Framework Core Join identity AspNetUser table. AspNetUser Id to custom tables/Entities

Example:

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
    }
John Pham
  • 65
  • 6

1 Answers1

0

Turns out that it was just like this

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

        }

John Pham
  • 65
  • 6