It was sudden trouble for me that MSSQL Server's auto-increment identity column doesn't work as I expected.
It doesn't guarantee a consistent sequence. I mean if it goes like 1, 2, 3, 4, 5 in case you restart your server it can easily continue like 10006, 10007, 10008, 10009, 10010. If you have some documents generated with these numbers (let say contracts) it can lead to the situation when your company had contracts with numbers 1, 2, 3, 4, 5 and then accidentally changed to 10006, 10007, 10008, 10009, 10010.
On other RDBMs the auto-increment column always keeps the consistency and it doesn't matter have you restarted it or not.
So now I want to change that column to be serviced by a sequence. The question is how can I do it maximum smoothly? I use Entity Framework and its migration mechanism.
The column is defined as:
CREATE TABLE [dbo].[
-- omitted for the sake of brevity
[CompanyNo] [bigint] IDENTITY(1,1) NOT NULL
-- omitted for the sake of brevity
);
My best guess is to create a migration:
- Getting the current latest value of the column
CompanyNo - Modifying the column to make it no auto-increment
- Creating the sequence with start value of what it was on step 1
Technologies:
- Entity Framework 6 + Migrations + Code First
- MSSQL
- ASP.NET MVC