efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Issue with Column Naming in Migrations for Snake Case

Open IlyaNavodkin opened this issue 1 year ago • 2 comments

Package Version:

  • Microsoft.EntityFrameworkCore Version 8.0.7
  • Microsoft.EntityFrameworkCore.Design Version 8.0.7
  • Microsoft.EntityFrameworkCore.Relational Version 8.0.7
  • Microsoft.EntityFrameworkCore.Tools Version 8.0.7
  • Npgsql.EntityFrameworkCore.PostgreSQL Version 8.0.4

Description I'm experiencing an issue when renaming columns in my migrations. Despite successfully renaming columns to snake_case, the migration process still attempts to insert values into the original camelCase column names.

Steps to Reproduce

Create a migration to rename a column in the migrations_history table from ProductVersion to product_version.

migrationBuilder.RenameColumn(
    name: "ProductVersion",
    schema: "universal_family",
    table: "migrations_history",
    newName: "product_version"
);

Run the migration using the command:

dotnet ef database update

Observe the following error in the logs:

Npgsql.PostgresException (0x80004005): 42703: column "ProductVersion" of relation "migrations_history" does not exist

The SQL statement being executed is:

INSERT INTO universal_family.migrations_history ("MigrationId", "ProductVersion") VALUES ('20241010144908_ChangeMigrationStyleNames', '8.0.7');

Expected Behavior I expect the migration process to utilize the updated column names, specifically using product_version instead of ProductVersion in SQL statements, like this:

INSERT INTO universal_family.migrations_history ("MigrationId", product_version) VALUES ('20241010144908_ChangeMigrationStyleNames', '8.0.7');

Actual Behavior The migration attempts to reference the old column names (ProductVersion), resulting in an error that indicates the column does not exist.

Additional Information

  • This behavior occurs despite correctly renaming the columns in the migration.
  • I have confirmed that the migration has been applied and that the database schema reflects these changes.
  • There appears to be a discrepancy between the snapshot of the migrations and the actual state of the database, which leads to this issue.

IlyaNavodkin avatar Oct 10 '24 15:10 IlyaNavodkin

Did you follow the instructions for customizing the migrations history table?

If so, please create a small runnable console project that illustrates the issue.

AndriySvyryd avatar Oct 11 '24 18:10 AndriySvyryd

Hello

https://github.com/IlyaNavodkin/MigrationCaseBug - sample

Use this flow:

  1. Create db for dbcontext and create init migration => dotnet ef migrations add InitMigration
  2. Run project (for miration). You have db with tables and migration table with one row history
  3. Add in dbcontext configuring this method UseSnakeCaseNamingConvention(); Image
  4. Create migration => dotnet ef migrations add ChangeCaseConvention
  5. Catch exception Image

IlyaNavodkin avatar Oct 15 '24 17:10 IlyaNavodkin

@IlyaNavodkin In the link posed above it states, "If you customize the Migrations history table after applying migrations, you are responsible for updating the existing table in the database." However, I don't see you doing that in your repro.

ajcvickers avatar Nov 13 '24 16:11 ajcvickers

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

ajcvickers avatar Nov 29 '24 17:11 ajcvickers