-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
SqlServer Migrations: Rebuild column when IDENTITY changes #2100
Comments
This is related to #2070 |
(Re-opening to track this separately from #329) |
Workaround from #20549 Rebuild the table:
The easiest way might be to just generate a script using SSMS and paste it into your migration: // UNDONE: Not supported by SQL Server
//migrationBuilder.AlterColumn<int>(
// name: "Id",
// table: "Areas",
// nullable: false,
// oldClrType: typeof(int),
// oldType: "int")
// .Annotation("SqlServer:Identity", "1, 1");
migrationBuilder.Sql(@"
CREATE TABLE [dbo].[tmp_ms_xx_...
SET IDENTITY_INSERT [dbo].[tmp_ms_xx_...
INSERT INTO [dbo].[tmp_ms_xx_...
SET IDENTITY_INSERT [dbo].[tmp_ms_xx_...
DROP TABLE [dbo].[...
EXECUTE sp_rename N'[dbo].[tmp_ms_xx_...
ALTER TABLE [dbo].[...
ADD FOREIGN KEY ([...
"); |
Another easy solution:
|
This would be nice to have. I'm having trouble trying to apply a migration due to this. |
Hi everybody, |
@mamaly12 this issue is in the backlog, so not planned for work at the moment. Please read the above comments for workarounds. |
Thank you @roji, but I think deleting a database or a table with big data is a very risky task, since you have to get a backup of everything and make sure to import all of data again. |
@mamaly12 That's precisely why this is not implemented. The only way to do this is to drop the table and rebuild it. That's a risky thing to do automatically, especially when metadata about the table may not be complete or accurate. |
Note the additional case of the identity seed/increment changing (#28377); although there's a way to change these without recreating the column, but apparently that doesn't persist across table truncations. |
Functional Impact
Changing a property to become an identity throws exception on migration
Minimal repro steps
Expected result
The migration is detecting that the column in the table isn't and identity in the database but needs to be to satisfy the model. Rather than generating and executing SQL that isn't valid, it would be more useful to detect the error and display a meaningful message to the user indicating which entity and property are causing the issue
Actual result
Migration fails with SqlException "Incorrect syntax near the keyword 'IDENTITY'."
The text was updated successfully, but these errors were encountered: