使用迁移重命名列时出现EF Core 2.0错误

问题描述 投票:1回答:2

我在重命名列和将更改迁移到数据库时遇到问题。

移民:

    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");
    }

我正在使用的命令(来自Package Manager Console / Powershell): Add-Migration RatingFix -p Gamgoo.Data.Context -c GamgooContext 更新数据库

并且错误消息:

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.

我已经检查了ef核心github,论坛和stackoverflow的类似问题,但这些答案并没有帮助我。 我想避免删除所有迁移和更新数据库,因为我已经在其他表中有相当多的数据。

database asp.net-core .net-core ef-migrations ef-core-2.0
2个回答
1
投票

这可能是由PR #11161修复的。你可以尝试the nightly builds

您可以通过重写sp_rename调用来解决此问题:

// 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';");

0
投票

尝试重命名我添加到身份用户的列时,我遇到了同样的问题。在检查数据库之后,我意识到我已经创建了迁移以生成原始列,但从未实际更新过数据库。早些时候,在某些挫折中,我删除了在我实际更新数据库时生成列的迁移。相当容易修复 - 手动创建原始列,然后使用重命名的当前迁移按预期工作。

希望我的回答可以帮助别人。

© www.soinside.com 2019 - 2024. All rights reserved.