Home

Override IDENTITY_INSERT for use in loading data through LINQ-to-SQL

edited December 2012
I was hoping to be able to batch load a bunch of records that already have the primary key specified and despite my best efforts in wrapping transactions and other things I can't seem to avoid the following error:
Explicit value must be specified for identity column in table 'Users' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.
Here is a code sample I have tried - is there a way to actually override the identity insert? I can do this in SQL but I am really trying to take the Joe Albahari LINQPad challenge and do this through LINQPad instead of the management studio in a more traditional script...

using (var t = new TransactionScope(TransactionScopeOption.Required)) { Users.Context.ExecuteCommand("SET IDENTITY_INSERT Users ON"); Users.InsertOnSubmit(new Users { Id=1234, Name="Goyuix"}); SubmitChanges(); Users.Context.ExecuteCommand("SET IDENTITY_INSERT Users OFF"); }

Comments

Sign In or Register to comment.