Closed fuzzykiller closed 2 years ago
Same issue when using Pomelo.EntityFrameworkCore.MySql provider - EF Core tries to rename existing indexes, even though their names are explicitly set via Fluent API. Fix is however pretty simple - remove calls to RenameIndex in migration file, but leave indexes with names that EF wants in Migration.Designer.cs file and ModelSnapshot, this way EF wouldn't try to rename those indexes in this and subsequent migrations. Even though this way EF would think that indexes have different names than they actually do have in DB, I don't think it would break something if you review your migrations' file which is a good practice anyway.
Description
When adding (via migration) a new index with the same explicit name as an existing index, indexes get needlessly renamed. Most of the time, the existing index gets renamed to Existing_Index1. Index names are per-table in SQL Server. There is no need to rename the new index, much less the existing one. This may be different on other database engines, like SQLite.
The existing logic, which is probably somewhere in the core, could move to the database adapter. Alternatively, the user could be made to handle this conflict.
Code
https://github.com/fuzzykiller/ef-core-migrations-index (The code is in a state where
dotnet ef migrations add AddHourIndex
can be run to demonstrate the behavior.)The relevant change, adding an index with the same name on a different entity:
Then running
dotnet ef migrations add AddHourIndex
results in the existing indexIX_Name_Unique
getting renamed toIX_Name_Unique1
.Verbose output
Provider and version information
EF Core version: 6.0.3 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0 Operating system: Windows 10 Enterprise 21H2 IDE: Rider 2021.3.3