EF Core 2.0 error while renaming column using Migrations

1

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.

database
asp.net-core
.net-core
ef-migrations
ef-core-2.0
asked on Stack Overflow Mar 19, 2018 by (unknown user)

2 Answers

1

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';");
answered on Stack Overflow Mar 20, 2018 by bricelam
0

I got this same issue while trying to rename a column that I had added to the identity User. After checking the database, I realized that I had created the migration to generate the original column, but never actually updated the database. Earlier in some frustration I had deleted the migration that would have generated the column had I actually updated the database. Fairly easy fix - manually created the original column and then the current migration with the rename worked as expected.

Hopefully my answer may help someone else.

answered on Stack Overflow Sep 4, 2018 by Aaron

User contributions licensed under CC BY-SA 3.0