efcore / EFCore.NamingConventions

Entity Framework Core plugin to apply naming conventions to table and column names (e.g. snake_case)
Apache License 2.0
715 stars 73 forks source link

TPH table names no longer use the DbSet property name in 8.0.1 #253

Closed aradalvand closed 8 months ago

aradalvand commented 8 months ago

Same as #247 except that TPH table names are still broken and the DbSet property name is being ignored.

Minimal repro:

The .csproj:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <RootNamespace>efcore_npgsql_json</RootNamespace>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="EFCore.NamingConventions" Version="8.0.1" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
    </ItemGroup>

</Project>

Program.cs:

using Microsoft.EntityFrameworkCore;
using Npgsql;

using var db = new AppDbContext();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();

class AppDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
        optionsBuilder
            .UseNpgsql("YOUR_CONNECTION_STRING")
            .UseSnakeCaseNamingConvention()
            .LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Information);

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Lesson>()
            .HasDiscriminator<string>("Type")
                .HasValue<VideoLesson>("video")
                .HasValue<ArticleLesson>("article");
    }

    public DbSet<Lesson> Lessons => Set<Lesson>();
}

public abstract class Lesson
{
    public int Id { get; set; }
    public required string Title { get; set; }
}

public class VideoLesson : Lesson
{
    public required string VideoId { get; set; }
}

public class ArticleLesson : Lesson
{
    public required string Content { get; set; }
}

Notice how the table's name ends up being lesson, rather than lessons:

info: 01/05/2024 11:54:22.489 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
      Executed DbCommand (106ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      DROP DATABASE idk_whatever WITH (FORCE);
info: 01/05/2024 11:54:23.300 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
      Executed DbCommand (712ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE DATABASE idk_whatever;
info: 01/05/2024 11:54:23.463 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
      Executed DbCommand (20ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE lesson (
          id integer GENERATED BY DEFAULT AS IDENTITY,
          title text NOT NULL,
          type character varying(8) NOT NULL,
          content text,
          video_id text,
          CONSTRAINT pk_lesson PRIMARY KEY (id)
      );
roji commented 8 months ago

sigh thanks for the repro @aradalvand, I'll fix this for 8.0.2.

roji commented 8 months ago

@aradalvand released 8.0.2 with a fix for this - please let me know how it works for you!

aradalvand commented 8 months ago

@roji Yep, this seems to have been fixed in 8.0.2; thank you! Another issue though :P #256