Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
BrunoBlanes opened this issue Dec 20, 2022 · 2 comments · Fixed by #30214
Closed

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

BrunoBlanes opened this issue Dec 20, 2022 · 2 comments · Fixed by #30214
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@BrunoBlanes
Copy link

BrunoBlanes commented Dec 20, 2022

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

@ajcvickers
Copy link
Member

Confirmed this is a bug with TPC mapping. Relational model with TPC is:

RelationalModel: 
  Table: Cat
    PK_Cat {'Id'} PrimaryKey
    EntityTypeMappings: 
      Cat - Cat !IncludesDerivedTypes
    Columns: 
      Id (int) NonNullable)
      EducationLevel (nvarchar(max)) NonNullable)
      Name (nvarchar(max)) NonNullable)
  Table: Dog
    PK_Dog {'Id'} PrimaryKey
    EntityTypeMappings: 
      Dog - Dog !IncludesDerivedTypes
    Columns: 
      Id (int) NonNullable)
      FavoriteToy (nvarchar(max)) NonNullable)
      Name (nvarchar(max)) NonNullable)
  Sequence: PetSequence Cyclic

Compare to TPT:

RelationalModel: 
  Table: MySchema.Cat
    PK_Cat {'Id'} PrimaryKey
    EntityTypeMappings: 
      Cat - Cat IncludesDerivedTypes
    Columns: 
      Id (int) NonNullable)
      EducationLevel (nvarchar(max)) NonNullable)
    ForeignKeyConstraints: 
      FK_Cat_Pet_Id Cat {'Id'} -> Pet {'Id'} Cascade
  Table: MySchema.Dog
    PK_Dog {'Id'} PrimaryKey
    EntityTypeMappings: 
      Dog - Dog IncludesDerivedTypes
    Columns: 
      Id (int) NonNullable)
      FavoriteToy (nvarchar(max)) NonNullable)
    ForeignKeyConstraints: 
      FK_Dog_Pet_Id Dog {'Id'} -> Pet {'Id'} Cascade
  Table: MySchema.Pet
    PK_Pet {'Id'} PrimaryKey
    EntityTypeMappings: 
      Pet - Pet IncludesDerivedTypes IsSharedTablePrincipal
      Cat - Pet !IncludesDerivedTypes !IsSharedTablePrincipal
      Dog - Pet !IncludesDerivedTypes !IsSharedTablePrincipal
    Columns: 
      Id (int) NonNullable)
        Annotations: 
          SqlServer:Identity: 1, 1
      Name (nvarchar(max)) NonNullable)

Repro:

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

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

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

public class SomeDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Database=AllTogetherNow")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("MySchema");
        modelBuilder.Entity<Pet>().UseTpcMappingStrategy();
        modelBuilder.Entity<Cat>();
        modelBuilder.Entity<Dog>();
    }
}

public class Program
{
    public static async Task Main()
    {
        using (var context = new SomeDbContext())
        {
            await context.Database.EnsureDeletedAsync();
            await context.Database.EnsureCreatedAsync();

            var model = context.GetService<IDesignTimeModel>();
            await context.SaveChangesAsync();
        }
    }
}

@ajcvickers ajcvickers added this to the 8.0.0 milestone Jan 12, 2023
@ajcvickers ajcvickers changed the title TPC and TPH mapping strategies not respecting default database schema during migration TPC mapping strategy not respecting default database schema during migration Jan 12, 2023
@ajcvickers
Copy link
Member

Poaching

@ajcvickers ajcvickers assigned ajcvickers and unassigned AndriySvyryd Feb 5, 2023
ajcvickers added a commit that referenced this issue Feb 5, 2023
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 5, 2023
ajcvickers added a commit that referenced this issue Feb 7, 2023
@ajcvickers ajcvickers removed this from the 8.0.0 milestone Feb 7, 2023
@ajcvickers ajcvickers reopened this Feb 7, 2023
@ajcvickers ajcvickers added this to the 7.0.x milestone Feb 9, 2023
@ajcvickers ajcvickers modified the milestones: 7.0.x, 7.0.4 Feb 14, 2023
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants