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

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

Open
kroatti opened this issue Mar 21, 2024 · 3 comments
Open

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

kroatti opened this issue Mar 21, 2024 · 3 comments

Comments

@kroatti
Copy link

kroatti commented Mar 21, 2024

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:

  • EFCore.NamingConventions Version="8.0.3"
  • Microsoft.EntityFrameworkCore Version="8.0.3"
  • Oracle.EntityFrameworkCore Version="8.21.121"
@vasicvuk
Copy link

vasicvuk commented May 22, 2024

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:

dotnet/efcore#33591

Maybe @roji can help with this one

@AndriySvyryd
Copy link

Duplicate of #191

@4broadcast
Copy link

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.

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

4 participants