databaseasp.net-core.net-coreentity-framework-migrationsef-core-2.0

EF Core 2.0 error while renaming column using Migrations


I've got problem with renaming column and migrating changes to database.

Migration:

    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.RenameColumn(
            name: "int",
            schema: "Gamgoo.More",
            table: "Rating",
            newName: "GivenRating");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.RenameColumn(
            name: "GivenRating",
            schema: "Gamgoo.More",
            table: "Rating",
            newName: "int");
    }

Commands that I'm using are (from Package Manager Console / Powershell):
Add-Migration RatingFix -p Gamgoo.Data.Context -c GamgooContext
Update-Database

And the error message:

Applying migration '20180319172151_RatingFix'.
Microsoft.EntityFrameworkCore.Migrations[200402]
      Applying migration '20180319172151_RatingFix'.
fail: Microsoft.EntityFrameworkCore.Database.Command[200102]
      Failed executing DbCommand (31ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      EXEC sp_rename N'Gamgoo.More.Rating.int', N'GivenRating', N'COLUMN';
System.Data.SqlClient.SqlException (0x80131904): Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
ClientConnectionId:ba25aa03-122d-4c55-9673-4bd3358f2f83
Error Number:15248,State:1,Class:11
Failed executing DbCommand (31ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
EXEC sp_rename N'Gamgoo.More.Rating.int', N'GivenRating', N'COLUMN';
System.Data.SqlClient.SqlException (0x80131904): Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:ba25aa03-122d-4c55-9673-4bd3358f2f83
Error Number:15248,State:1,Class:11
Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.

I've checked ef core github, forum and stackoverflow for similar problems, but those answers did not helped me.
I would like to avoid removing all migrations and updating the database, because I already have quite a lot of data in other tables.


Solution

  • This may have been fixed by PR #11161. You can try the nightly builds.

    You can work around the issue by rewriting the sp_rename call:

    // UNDONE: SQL generated by EF Core is missing schema identifier quotes
    //migrationBuilder.RenameColumn(
    //     name: "int",
    //     schema: "Gamgoo.More",
    //     table: "Rating",
    //     newName: "GivenRating");
    migrationBuilder.Sql(
        "EXEC sp_rename N'[Gamgoo.More].[Rating].[int]', N'GivenRating', N'COLUMN';");