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

Snake case naming convention causes incorrect table names for TPH hierarchies #310

Open
benlongo opened this issue Feb 12, 2025 · 0 comments

Comments

@benlongo
Copy link

benlongo commented Feb 12, 2025

Cross-posting this from ef core repo: dotnet/efcore#35604

Repro:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

await using var ctx = new MyContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

Console.WriteLine( "With instantiated DbContext:" );
foreach( var et in ctx.Model.GetEntityTypes() ) {
    var tn = et.GetTableName();
    Console.WriteLine( $"{et.ClrType.Name} -> {tn}" );
}

public class MyContext : DbContext {
    public DbSet<MyEntity> Entities { get; set; }

    protected override void OnModelCreating( ModelBuilder modelBuilder ) {
        modelBuilder.Entity<MyEntity>()
            .HasDiscriminator<string>( "entity_type" )
            .HasValue<MyDerivedEntity1>( "1" )
            .HasValue<MyDerivedEntity2>( "2" );

        modelBuilder.Entity<MyEntity>()
            .ToTable( "entity_table" );

        modelBuilder.Entity<MyDerivedEntity1>()
            .HasBaseType<MyEntity>();

        modelBuilder.Entity<MyDerivedEntity2>()
            .HasBaseType<MyEntity>();

        Console.WriteLine( "Within OnModelCreating:" );
        foreach( IMutableEntityType mutableEntityType in modelBuilder.Model.GetEntityTypes() ) {
            var tableName = mutableEntityType.GetTableName();
            Console.WriteLine( $"{mutableEntityType.ClrType.Name} -> {tableName}" );
        }
    }

    protected override void OnConfiguring( DbContextOptionsBuilder optionsBuilder ) {
        optionsBuilder
            .UseSqlite( "DataSource=:memory:" )
            .EnableSensitiveDataLogging()
            .UseSnakeCaseNamingConvention();
    }
}

public abstract class MyEntity {
    public int Id { get; set; }
    public string Shared { get; set; }
}

public class MyDerivedEntity1 : MyEntity {
    public string Foo { get; set; }
}

public class MyDerivedEntity2 : MyEntity {
    public string Bar { get; set; }
}

Output:

Within OnModelCreating:
MyDerivedEntity1 -> entities
MyDerivedEntity2 -> entities
MyEntity -> entity_table
Within OnModelCreating:
MyDerivedEntity1 -> entities
MyDerivedEntity2 -> entities
MyEntity -> entity_table
With instantiated DbContext:
MyDerivedEntity1 -> entity_table
MyDerivedEntity2 -> entity_table
MyEntity -> entity_table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant