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

Unexpected RenameIndex added to EF Core migration after upgrading from EF Core 3.1 to EF Core 6 #30231

Closed
cyclr-adrian opened this issue Feb 8, 2023 · 5 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@cyclr-adrian
Copy link

I've upgraded my project references from:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.31" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.31">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.31" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.31" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.31" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.31">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>

<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.7" />

to:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.12" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.12">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.12" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.12" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.12" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.12">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>

<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />

Now when I add a new database entity to my DbContext and run Add-Migration ... I get a load of RenameIndex calls in my Up and Down migration for existing tables, and I'm not sure why?

For example I have an existing table called trigger which has an index called IX_ConnectorEntry_Id but in the added migration Up method it wants to rename it to IX_trigger_ConnectorEntry_Id and in the Down method it wants to put it back to what it was before.
See:

        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.RenameIndex(
                name: "IX_ConnectorEntry_Id",
                table: "trigger",
                newName: "IX_trigger_ConnectorEntry_Id");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.RenameIndex(
                name: "IX_trigger_ConnectorEntry_Id",
                table: "trigger",
                newName: "IX_ConnectorEntry_Id");
        }

It's trying to add the database name into the index name for some reason and its doing this for 111 existing indexes. The only way I can seem to stop this from happening is to add an explicit HasDatabaseName(...) in the entity configuration.
See:

    public class TriggerMap : IEntityTypeConfiguration<Trigger>
    {
        public void Configure(EntityTypeBuilder<Trigger> builder)
        {
            builder.HasIndex(t => t.ConnectorEntry_Id).HasDatabaseName("IX_ConnectorEntry_Id");
        }
    }

I've not found anywhere online that states there is a breaking change to the way default index names are generated when upgrading from EF Core 3.1 to EF Core 6.

Are the only options here to give in and let EF Core dictate my index names or have to manually set them with the explicit HasDatabaseName(...) ?

@ajcvickers
Copy link
Member

@AndriySvyryd Is this related to #18958?

@ajcvickers
Copy link
Member

@cyclr-adrian I am not able to reproduce this. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

@cyclr-adrian
Copy link
Author

@ajcvickers I have created a cutdown version of my solution which reproduces the issue.

https://github.com/cyclr-adrian/efcore-30231

@ajcvickers
Copy link
Member

@cyclr-adrian Yes, I can see that the index with that name is created in EF Core 6. But for me, the same name is used on EF Core 3.1:

            migrationBuilder.CreateIndex(
                name: "IX_trigger_ConnectorEntry_Id",
                table: "trigger",
                column: "ConnectorEntry_Id");

@ajcvickers
Copy link
Member

Regardless, it should be fairly safe to process all indexes at the end of OnModelCreating to set their names appropriate. For example:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());

    foreach (var index in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetIndexes()))
    {
        index.SetDatabaseName($"IX_{index.Properties.Single().Name}");
    }
}

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Feb 22, 2023
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants