You need to take all the steps in one block of T-SQL code - it will be very difficult, if not impossible, if you want to enable it, and then run the LINQ-to-SQL query, and then rotate it back off :(
The only real solution I see is to pack all of the SQL into an SQL statement and execute this:
SET IDENTITY_INSERT MyTable ON (do your update here) SET IDENTITY_INSERT MyTable OFF
and execute it as one block of code using .ExecuteContext()
Mark
PS: for your EDIT # 2: no, unfortunately, there is no (simple) way to remove an identifier from a column and enable it again. Basicall you will need to create a new column without IDENTITY, copy the values, reset the IDENTITY column and then do the same back when you are done - sorry !: - (
PS # 2: this really asks the question: why do I need an โidentity insertโ? On a regular basis, from the application? Provided - you may encounter this need from time to time, but I always did it separately, in Mgmt Studio SQL - of course, not in my application ..... (just curious what your use case / motivation is).
source share