Skip to content

TPC mapping strategy not respecting default database schema during migration #29899

@BrunoBlanes

Description

@BrunoBlanes

File a bug

When using the new .UseTpcMappingStrategy() and .UseTphMappingStrategy() extension methods the default database schema is not respected:

Model

public abstract class Pet
{
    public string Name { get; set; }
}

public class Cat : Pet
{
    public string EducationLevel { get; set; }
}

public class Dog : Pet
{
    public string FavoriteToy { get; set; }
}

DbContext

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.HasDefaultSchema("MySchema");

    // Uses "MySchema"
    modelBuilder.Entity<Pet>()
	.UseTptMappingStrategy();

    // Uses "dbo"
    modelBuilder.Entity<Pet>()
	.UseTpcMappingStrategy();
    modelBuilder.Entity<Pet>()
	.UseTphMappingStrategy();
}

Migration

migrationBuilder.CreateTable(
    name: "Dogs",
    // Omits the 'schema' parameter
    columns: table => new { } [...])

Workaround

Explicitly specify the schema either by applying the TableAttribute on every inherited class:

[Table("Dogs", Schema = "MySchema")]
public class Dog : Pet

Or via the Fluent API:

modelBuilder.Entity<Dog>().ToTable("Dogs", "MySchema");

Provider and version information

EF Core version: 7.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.5 Preview 2

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions