EF Core 5 with TPT for IdentityUser

94 Views Asked by At

I'm trying to setup a project with multiple IdentityUsers. With EF 5 Core (code first) it should be possible to use TPT (Table Per Type). But for some reason the entity is created in a single table.

This is my code:

public class AppDbContext : IdentityDbContext<User, Role, int>
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Teacher>().ToTable("Teachers");
        modelBuilder.Entity<Student>().ToTable("Students");
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<Teacher> Teachers { get; set; }
    public DbSet<Student> Students { get; set; }
}

public class User : IdentityUser<int>
{
}

public class Role : IdentityRole<int>
{
}

public class Teacher : User
{
    public string Name { get; set; }
    public string JobTitle { get; set; }
}

public class Student : User
{
    public string Name { get; set; }
    public string Major { get; set; }
}

When I run Add-Migration, it then creates a migration where both the Teacher and Student properties are placed in the AspNetUsers table.

Shouldn't it create a separate table for Student and Teacher when using EF Core 5?

0

There are 0 best solutions below