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

Redundant FK constraint when using TPT inheritance and owned types #270

Open kroatti opened 5 months ago

kroatti commented 5 months ago

I'm using table per type inheritance model and owned types in the same class hierarchy. When I generate the migration an extra foreign key constraint will be added to the derived type. Executing the update database returns an error:

'ORA-02274: duplicate referential constraint specifications'

The model:

public abstract class BaseType
{
   public Guid Id { get; set; }
}

public class DerivedType : BaseType
{
   public OwnedType Prop1 { get; set; }
}

public class OwnedType 
{
   public int Prop2 { get; set; }
}

Model configuration:

modelBuilder.Entity<BaseType>()
    .ToTable("BASETYPES");

modelBuilder.Entity<DerivedType>()
    .ToTable("DERIVEDTYPES")
    .OwnsOne(e => e.Prop1);

DbContext builder:

builder.Services.AddOracle<OraDb>("CONNSTR", null,
    optionsBuilder => optionsBuilder.UseUpperCaseNamingConvention());

Generated migration:

migrationBuilder.CreateTable(
    name: "BASETYPES",
    columns: table => new
    {
        ID = table.Column<Guid>(type: "RAW(16)", nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_BASETYPES", x => x.ID);
    });

migrationBuilder.CreateTable(
    name: "DERIVEDTYPES",
    columns: table => new
    {
        ID = table.Column<Guid>(type: "RAW(16)", nullable: false),
        PROP1_PROP2 = table.Column<int>(type: "NUMBER(10)", nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_DERIVEDTYPES", x => x.ID);
        table.ForeignKey(
            name: "FK_DERIVEDTYPES_BASETYPES_ID",
            column: x => x.ID,
            principalTable: "BASETYPES",
            principalColumn: "ID",
            onDelete: ReferentialAction.Cascade);
        table.ForeignKey(
            name: "FK_DERIVEDTYPES_DERIVEDTYPES_ID",
            column: x => x.ID,
            principalTable: "BASETYPES",
            principalColumn: "ID",
            onDelete: ReferentialAction.Cascade);
    });

FK_DERIVEDTYPES_DERIVEDTYPES_ID is redundant and its name is incorrect. When I don't use UseUpperCaseNamingConvention() or UseSnakeCaseNamingConvention() the migration is generated properly.

Used packages:

vasicvuk commented 3 months ago

I have the same issue with SQL Server i created a reproduction here:

https://github.com/vasicvuk/ef-core-issue-33591

I also reported this to the EF community since I didn't know where the issue is:

https://github.com/dotnet/efcore/issues/33591

Maybe @roji can help with this one

AndriySvyryd commented 2 months ago

Duplicate of #191

4broadcast commented 1 month ago

Hi, just hit same issue with a TPT hierarchy & UseSnakeCaseNamingConvention(), The migration will not generate the redundant constraints with default EF Core casing. Also on 8.0.3.