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

SqlServer Migrations: Rebuild column when IDENTITY changes #2100

Open
Tracked by #22946
stuartleeks opened this issue May 1, 2015 · 13 comments
Open
Tracked by #22946

SqlServer Migrations: Rebuild column when IDENTITY changes #2100

stuartleeks opened this issue May 1, 2015 · 13 comments

Comments

@stuartleeks
Copy link

Functional Impact

Changing a property to become an identity throws exception on migration

Minimal repro steps

  1. Add an entity with an integer non-key property
  2. add and apply a migration to create the table for the entity
  3. Configure the property to be the key and an identity field
  4. Add and apply a migration. At this point you will get the SqlException

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'."

@bricelam
Copy link
Contributor

This is related to #2070

@bricelam
Copy link
Contributor

bricelam commented Aug 7, 2015

Enabling this scenario, would require a table rebuild (issue #329). Once #2070 is fixed, EF7 behavior will match EF6. I'm closing this for now with a note on #329 to consider doing it in the future.

@bricelam
Copy link
Contributor

bricelam commented Apr 7, 2020

(Re-opening to track this separately from #329)

@bricelam bricelam reopened this Apr 7, 2020
@bricelam bricelam changed the title Changing a property to become an identity throws exception on migration SqlServer Migrations: Rebuild column when IDENTITY changes Apr 7, 2020
@bricelam
Copy link
Contributor

bricelam commented Apr 7, 2020

Additional duplicates: #3993 #7444

@ajcvickers
Copy link
Member

Workaround from #20549

Rebuild the table:

  1. Add a new table with IDENTITY on the column
  2. Copy data from the old table to the new table (with IDENTITY_INSERT ON)
  3. Drop referencing constraints and indexes
  4. Drop the old table
  5. Rename the new table back to its original name
  6. Re-create referencing constraints and indexes

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 ([...
");

@ajcvickers ajcvickers added this to the Backlog milestone Apr 17, 2020
@emreerkoca
Copy link

Another easy solution:

  1. Delete all migrations.
  2. Delete database
  3. Add migration
  4. Update database :)

@bricelam
Copy link
Contributor

Computer GIF

@TAGC
Copy link

TAGC commented Apr 23, 2021

This would be nice to have. I'm having trouble trying to apply a migration due to this.

@mamaly12
Copy link

Hi everybody,
Has this issue been fixed yet?
I still facing with this error: "To change the IDENTITY property of a column, the column needs to be dropped and recreated."

@roji
Copy link
Member

roji commented Sep 13, 2021

@mamaly12 this issue is in the backlog, so not planned for work at the moment. Please read the above comments for workarounds.

@mamaly12
Copy link

mamaly12 commented Sep 13, 2021

@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.

@ajcvickers
Copy link
Member

@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.

@roji
Copy link
Member

roji commented Jul 11, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants