Question

I have just started converting an old web application into a new structure.

I have a newly designed supplier table created in a MS SQL Server 2012 DB, which I am in the process of transferring the supplier data from the old DB to.

I have a C# MVC application set-up using the new DB as the data context. At present the application gets the data from the old applications DB, checks it, reformats it where necessary and inserts it into the new database in the desired format.

However as users of the application have got used to the supplier IDs in the old DB, I don't want to change the IDs, however as the id field in the new supplier table is an auto increment, it doesn't copy the ids from the old database. It just simply increments.

Does anybody know of anyway the auto increment can be overridden programmatically in the c# code for the initial import stage of the project. Once the data from the old system has been imported, I would then like to revert back to auto increment.

Was it helpful?

Solution 2

You can turn off the identity generation on the server temporarily during the bunch of inserts, then turn it on again. Try submitting this query to the server:

SET IDENTITY_INSERT dbo.sometable ON ;

INSERT INTO dbo.sometable (id,.......) VALUES (456,.......) ;
INSERT INTO dbo.sometable (id,.......) VALUES (276,.......) ;
INSERT INTO dbo.sometable (id,.......) VALUES (387,.......) ;

SET IDENTITY_INSERT dbo.sometable OFF ;

It just disables identity creation, then does the inserts (note that the column and value lists do include the id), then restores the default behavior.

OTHER TIPS

You can set identity insert on. However, if the users know the keys I've found they start asking to change them. I might consider the old keys as the user identifier and create a new surrogate key they won't ever see.

In Sql Server, you can set the ID field to non identity. This will make it not auto increment

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top