Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations: table rename throws an exception #24175

Closed
dpihac opened this issue Feb 17, 2021 · 1 comment · Fixed by #32599
Closed

Migrations: table rename throws an exception #24175

dpihac opened this issue Feb 17, 2021 · 1 comment · Fixed by #32599
Assignees
Labels
area-migrations area-sqlserver closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-6.0 type-bug

Comments

@dpihac
Copy link

dpihac commented Feb 17, 2021

I have created new migration which produced following code.
Table is to be renamed from Image to Images.

  public partial class ImagesRename : Migration
  {
      protected override void Up(MigrationBuilder migrationBuilder)
      {
          migrationBuilder.DropForeignKey(
              name: "FK_Image_Items_OwnerId",
              table: "Image");

          migrationBuilder.DropPrimaryKey(
              name: "PK_Image",
              table: "Image");

          migrationBuilder.RenameTable(
              name: "Image",
              newName: "Images");

          migrationBuilder.RenameIndex(
              name: "IX_Image_OwnerId",
              table: "Images",
              newName: "IX_Images_OwnerId");

          migrationBuilder.AddPrimaryKey(
              name: "PK_Images",
              table: "Images",
              column: "Id");

          migrationBuilder.AddForeignKey(
              name: "FK_Images_Items_OwnerId",
              table: "Images",
              column: "OwnerId",
              principalTable: "Items",
              principalColumn: "Id",
              onDelete: ReferentialAction.Restrict);
      }
 }

After running update-database, following exception was thrown:

Applying migration '20210217173637_ImagesRename'.
Failed executing DbCommand (107ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
EXEC sp_rename N'[Image]', N'Images';
Microsoft.Data.SqlClient.SqlException (0x80131904): This action cannot be performed on a system type.
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) in H:\tsaagent1\_work\21\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1777
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) in H:\tsaagent1\_work\21\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\SqlInternalConnection.cs:line 606
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) in H:\tsaagent1\_work\21\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\TdsParser.cs:line 1302
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) in H:\tsaagent1\_work\21\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\TdsParser.cs:line 2523
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite) in H:\tsaagent1\_work\21\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\SqlCommand.cs:line 3043
   at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName) in H:\tsaagent1\_work\21\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\SqlCommand.cs:line 1483
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery() in H:\tsaagent1\_work\21\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\SqlCommand.cs:line 1040
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
   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 connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:b8cf77b7-9335-4dc2-84d3-bdd08c329f5d
Error Number:4185,State:1,Class:16
This action cannot be performed on a system type.

I tried to run the T-SQL on the target DB to make sure

EXEC sp_rename N'[Image]', N'Images';

Following error was reported back:

Msg 4185, Level 16, State 1, Procedure sp_rename, Line 186 [Batch Start Line 0]
This action cannot be performed on a system type.

Completion time: 2021-02-17T18:41:26.4619026+01:00

Workaround

Following T-SQL code is workaround:

EXEC sp_rename 'Database.Schema.TableName', 'NewTableName';

I have run EXEC sp_rename 'dbo.Image', 'Images'; which successfully renamed the table.

Another solution would be to manually edit migration and add explicit schema to the migration:

migrationBuilder.RenameTable(
    name: "Image", 
    schema: "dbo",
    newName: "Images", 
    newSchema: "dbo");

Provider and version information

EF Core version: 5.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5
Operating system: Windows 10
IDE: Visual Studio 2019 18.6.5

@bricelam
Copy link
Contributor

bricelam commented May 5, 2021

Good bug. We should add special handing for tables with names that match system types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations area-sqlserver closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-6.0 type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants