Why is Entity Framework (6) code-first inheritance strategy defaulting to TPT not TPH?

Viewed 9

According to everything I've read, the default EF code-first inheritance strategy is Table-Per-Hierarchy, but that is not what I'm getting - I'm getting TPT. (I'm on .NET 6.)

For the sake of this post, I've reproduced my issue with MS's own simplistic example:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

public class RssBlog : Blog
{
    public string RssUrl { get; set; }
}

public partial class MyContext : DbContext
{
  ...
  public DbSet<Blog> Blogs { get; set; }
  public DbSet<RssBlog> RssBlogs { get; set; }
  ...
}

But when I run add-migration Add_Blog_Tables I get the migration below...TPT not TPH! Can anyone think of anything that might be causing my DbContext to default to TPH? And how would I tell EF that I want TPH when it is defaulting to TPT?

Thanks.

protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Blog",
            columns: table => new
            {
                BlogId = table.Column<int>(type: "int", nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                Url = table.Column<string>(type: "nvarchar(max)", nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Blog", x => x.BlogId);
            });

        migrationBuilder.CreateTable(
            name: "RssBlog",
            columns: table => new
            {
                BlogId = table.Column<int>(type: "int", nullable: false),
                RssUrl = table.Column<string>(type: "nvarchar(max)", nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_RssBlog", x => x.BlogId);
                table.ForeignKey(
                    name: "FK_RssBlog_Blog_BlogId",
                    column: x => x.BlogId,
                    principalTable: "Blog",
                    principalColumn: "BlogId");
            });
    }
0 Answers
Related