efcore / EFCore.NamingConventions

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

TPC inheritance fails with an abstract parent #177

Closed amyboose closed 1 year ago

amyboose commented 1 year ago

I've got an exception after using inheritance:

using Microsoft.EntityFrameworkCore;

namespace EfCoreTpc;
public class Program
{
    public static async Task Main(params string[] args)
    {
        IHost host = Host.CreateDefaultBuilder()
        .ConfigureServices(services =>
        {
            services.AddDbContext<MyContext>(builder =>
            {
                builder.UseNpgsql("Host=localhost;Port=7435;Database=testdb;Username=admin;Password=testpass")
                .UseSnakeCaseNamingConvention();
            });
        })
        .Build();

        await host.RunAsync();
    }
}

public abstract class Parent
{
    public int Id { get; set; }
}

public class Child : Parent
{
}

public class MyContext : DbContext
{
    public DbSet<Child> Childs{ get; set; }

    public MyContext(DbContextOptions options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Parent>(builder =>
        {
            builder.UseTpcMappingStrategy();
        });
    }
}

The exception exists just in TPC inheritance. Exception message:

The corresponding CLR type for entity type 'Parent' cannot be instantiated, but the entity type was mapped to 'parent' using the 'TPC' mapping strategy. Only instantiable types should be mapped. See https://go.microsoft.com/fwlink/?linkid=2130430 for more information.

Dubzer commented 1 year ago

I'm trying to make a similar setup and ran into this problem. You should specify DbSet for Parent as well.

Unfortunately, TPC is currently broken with this library, so I wouldn't recommend using it Check https://github.com/efcore/EFCore.NamingConventions/issues/154

roji commented 1 year ago

@AndriySvyryd should RelationalEntityTypeExtensions.GetDefaultTableName return null for abstract types in a TPC hierarchy? That function seems to already be aware of inheritance mapping (returning the root table name for TPH).

roji commented 1 year ago

@Dubzer @amyboose I'll be releasing 7.0.2 shortly which should make at least basic TPC work properly (modulo e.g. #154).

AndriySvyryd commented 1 year ago

@AndriySvyryd should RelationalEntityTypeExtensions.GetDefaultTableName return null for abstract types in a TPC hierarchy? That function seems to already be aware of inheritance mapping (returning the root table name for TPH).

Yes, by default we don't map abstract types in a TPC hierarchy to any table