efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Entity Framework Core creates unnecessary foreign key for owned type when using TPT mapping strategy

Open vasicvuk opened this issue 10 months ago • 1 comments

File a bug

I found the original issue on Stackoverflow here:

https://stackoverflow.com/questions/75614840/entity-framework-core-creates-unnecessary-foreign-key-for-owned-type-when-using

I have the same issue, so I am pasting issue here:

When I configure any owned type in TPT child entity, ef creating additional foreign key to parent table. I have configuration like this:

modelBuilder.Entity<TransportBase>(e => {
  e.UseTptMappingStrategy();
  e.ToTable("transports");
})
modelBuilder.Entity<Car>(e => {
  e.OwnsOne(x => x.MaxSpeed, bld =>
  {
    bld.Property(x => x.Value)
      .UsePropertyAccessMode(PropertyAccessMode.Field)
      .HasColumnName("max_speed");
  });
  e.HasBaseType<TransportBase>();
  e.ToTable("cars"));
})

And it generates these constraints in table "cars":

constraints: table =>
                {
                    table.PrimaryKey("PK_cars", x => x.id);
                    table.ForeignKey(
                        name: "fk_cars_transports_id",
                        column: x => x.id,
                        principalTable: "transports",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "fk_transports_transports_id",
                        column: x => x.id,
                        principalTable: "transports",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

It is the same keys, except for the name. As far as I could figure it out, this is because the key for owned-type points to the primary key for Car. But the Id field flies into TransportBase, so ef starts thinking the owner is in another table and creates a foreign key. How can I fix it?

I tried to set shadow property, which will be principal key, but it creates unnecessary columns in table.

vasicvuk avatar Apr 22 '24 15:04 vasicvuk

Please try EF 9.0.0-preview.3.24172.4

If you are still experiencing the issue share a small runnable project that shows it.

AndriySvyryd avatar Apr 27 '24 01:04 AndriySvyryd

Hi @AndriySvyryd

We are using Entity Framework 8, is there also a fix for the EF Core 8 version? I didn't try EF 9 because I also use Oracle Devart driver which is not available for EF 9

vasicvuk avatar May 13 '24 11:05 vasicvuk

@vasicvuk Could you share a small runnable project that shows the issue? If possible, use the SqlServer provider

AndriySvyryd avatar May 15 '24 01:05 AndriySvyryd

Hi @AndriySvyryd I created a small repro on this repository: https://github.com/vasicvuk/ef-core-issue-33591

However i don't know if the issue is in EF Core or in EFCore.NamingConventions library because when UseCamelCaseNamingConvention is not used, everything works fine.

vasicvuk avatar May 22 '24 14:05 vasicvuk

This is indeed an issue in EFCore.NamingConventions. Same root cause as https://github.com/efcore/EFCore.NamingConventions/issues/191

AndriySvyryd avatar Jun 19 '24 20:06 AndriySvyryd