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

TPC: generated index use parent name #154

Open
benedict-odonovan opened this issue Sep 2, 2022 · 2 comments
Open

TPC: generated index use parent name #154

benedict-odonovan opened this issue Sep 2, 2022 · 2 comments
Labels
blocked bug Something isn't working
Milestone

Comments

@benedict-odonovan
Copy link

Was pointed here from the dotnet/efcore repository.

The preview versions of EF7 support Table per Concrete Type inheritance which is currently incompatible with .UseSnakeCaseNamingConvention. Generated indexes use the name of the parent type rather than that of the child type, causing migrations to fail because of duplicate index names.

Entity Models:

public abstract class BaseEntityTrackable<TKey>
{
    public TKey Id { get; set; }
    public DateTime CreatedOn { get; set; }
    public int CreatedById { get; set; }
    public Consultant CreatedBy { get; set; }

    public DateTime EditedOn { get; set; }
    public int EditedById { get; set; }
    public Consultant EditedBy { get; set; }
}

[Table("candidates")]
public class Candidate : BaseEntityTrackable<int>
{
    public string FirstName { get; set; }
}

[Table("addresses")]
public class Address : BaseEntityTrackable<int>
{
    public string? PostCode { get; set; }
}

[Table("consultants")]
public class Consultant
{
    public int Id { get; set; }
}

In EntityContext.cs:

public DbSet<BaseEntityTrackable<int>> BaseEntityTrackables { get; set; }
public DbSet<Candidate> Candidates { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<Consultant> Consultants { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<BaseEntityTrackable<int>>().UseTpcMappingStrategy();

    modelBuilder.Entity<BaseEntityTrackable<int>>().Property(e => e.CreatedOn).HasDefaultValueSql("current_timestamp").ValueGeneratedOnAdd();
    modelBuilder.Entity<BaseEntityTrackable<int>>().Property(e => e.EditedOn).HasDefaultValueSql("current_timestamp").ValueGeneratedOnAddOrUpdate();
}

In Startup.cs:

builder.Services.AddDbContext<EntityContext>(options =>
    options.UseSnakeCaseNamingConvention()
    );

In the generated migration:

migrationBuilder.CreateIndex(
                name: "ix_base_entity_trackables_created_by_id",
                table: "addresses",
                column: "created_by_id");

            migrationBuilder.CreateIndex(
                name: "ix_base_entity_trackables_edited_by_id",
                table: "addresses",
                column: "edited_by_id");

            migrationBuilder.CreateIndex(
                name: "ix_base_entity_trackables_created_by_id",
                table: "candidates",
                column: "created_by_id");

            migrationBuilder.CreateIndex(
                name: "ix_base_entity_trackables_edited_by_id",
                table: "candidates",
                column: "edited_by_id");

When update-database is run the following error is returned:
42P07: relation "ix_base_entity_trackables_created_by_id" already exists

System Info:
EF Core version: 7.0.0-preview.7.22376.2
EFCore.NamingConventions version: 6.0.0
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL:7.0.0-preview.7
Target framework: (e.g. .NET 5.0) .NET 6.0
Operating system: Windows 10
IDE: (e.g. Visual Studio 2019 16.3) Visual Studio 2022 17.3

@AndriySvyryd
Copy link

Fixing this properly won't be possible until dotnet/efcore#27973, dotnet/efcore#27972 and dotnet/efcore#27971 are fixed. The best that can be done for EF 7 is skipping objects on entity types using TPC or entity splitting

@roji roji added this to the Backlog milestone Sep 3, 2022
@roji roji changed the title Support for TPC inheritance in EF7 TPC: generated index use parent name Jan 13, 2023
@roji roji added the blocked label Jan 3, 2024
@roji
Copy link
Member

roji commented Jan 3, 2024

Note that this was also filed as #185. Until EF-side support is available, I've at least removing the rewriting for 8.0.0 - index names in this case aren't rewritten, but at least they don't cause duplicate name errors. Keeping this issue to track properly rewriting.

@roji roji added the bug Something isn't working label Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants