4

I've spent last couple of days looking here and here and here among others, but these don't clearly apply to my case, where I'm trying to update a database after a successful.

dotnet ef migrations add RenameColumnAlert_Identifier

In Microsoft.AspNetCore.All (2.0.7) MVC application. I'm using MySql.Data.EntityFrameworkCore (6.10.6) and I thoroughly edited the Migration Up and the ApplicationDbContextModelSnapshot files before attempting the database update. But I still get the Exception copied into my question.

Basically I changed 'Identifier' to 'Alert_Identifier' in Alert.cs class and its associated db table and used 'Alert_Identifier' as foreign key for three other classes/tables.

Migration:

protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "Identifier",
                table: "Alert");

            migrationBuilder.RenameColumn(
                name: "language",
                table: "Info",
                newName: "Language");

            migrationBuilder.RenameColumn(
                name: "Language",
                table: "Alert",
                newName: "Alert_Identifier");

            migrationBuilder.AddColumn<string>(
                name: "Alert_Identifier",
                table: "Resource",
                nullable: false);

            migrationBuilder.AddColumn<string>(
                name: "Alert_Identifier",
                table: "Info",
                nullable: false);

            migrationBuilder.AddColumn<string>(
                name: "Alert_Identifier",
                table: "Area",
                nullable: false);
        }

Specifically, the Stack Trace starts with: at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(RenameColumnOperation operation, IModel model, MigrationCommandListBuilder builder)

This seems to indicate that there is something amiss with the RenameColumnoperation. Is this not yet implemented? Is there a workaround? Any help would be greatly appreciated.

Pawan Lakhara
  • 1,146
  • 4
  • 16
  • 28
Rex B
  • 43
  • 8
  • I'm stuck with the same problem. Did you find out any solution? – Rafael Osuna Dominguez Nov 19 '18 at 18:44
  • 4
    Hi @Rafael Osuna Dominguez, sorry I didn't get to this till now. It turned out that RenameColumn() isn't supported, so I had to DropColumn()/AddColumn(() for each column I wanted to rename. – Rex B Nov 22 '18 at 18:08

2 Answers2

0

This still isn't implemented in the MySql.EntityFrameworkCore (5.0.5) however the suggestion to just modify the migration and change RenameColumn to a DropColumn and AddColumn works however you're going to lose data that way of course.

There is an option and that is to use Pomelo's version as it is a bit more feature rich and regularly maintained - Pomelo.EntityFrameworkCore.MySql will come up as a Nuget package, or alternatively here is the Project URL: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

Rusty
  • 1
  • 1
0

I know this isn't the ideal solution. But while the method is not supported why not switch to raw SQL.

So for example delete this

  migrationBuilder.RenameColumn(
                name: "language",
                table: "Info",
                newName: "Language");

and replace with

    migrationBuilder.Sql(@"ALTER TABLE `Info` 
                            CHANGE COLUMN `language` `Language`;");

This approach has the benefit of avoiding any data loss problems.

Dave Barnett
  • 2,045
  • 2
  • 16
  • 32