views:

160

answers:

2

I am trying to do record inserts on a table where the primary key is an Identity field.

I have tried calling mycontext.ExecuteCommand("SET identity_insert myTable ON") but this doesn't do any good.

I get an error saying that identity_insert is off when I submit changes.

How can I turn it ON from the c# code before I submit changes?

EDIT

I have read that this is because ExecuteCommand's code gets executed in a different session.

EDIT 2

Is there any way I can execute some DDL to remove the Identity Specification from my C# code, do the inserts, and then turn Identity Specification back on?

+3  A: 

You need to do all the steps in a single T-SQL code block - which is going to be really hard if not impossible if you want to turn it on, then execute your LINQ-to-SQL query, and then turn it back off :(

The only real solution I see is to package up the entire SQL into a SQL statement and execute that:

SET IDENTITY_INSERT MyTable ON

(do your update here)

SET IDENTITY_INSERT MyTable OFF

and execute that as a single code block using .ExecuteContext()

Marc

PS: for your EDIT#2 : no, unfortunately, there's no (easy) way to remove the identity from a column, and turn it back on. Basicall you'd have to create a new column without the IDENTITY, copy the values over, drop the IDENTITY column and then do the same backwards when you're done - sorry! :-(

PS #2: this really begs the question: what on earth to do need to do an "identity insert" for? On a regular basis, from an app? Granted - you might run into this need once in a while, but I'd always do this separately, in SQL Mgmt Studio - certainly not in my app..... (just curious what your use case / motivation is).

marc_s
See edit #2
Ronnie Overby
@PS#2 read this: http://stackoverflow.com/questions/1368494/transfer-transform-data-from-sql-server-2008-db-to-anotherI was going to go ahead and try to code the work, but I may end up learning SSIS. Any advice?
Ronnie Overby
+1 for asking "why?"
gbn
I never said it was for a regular basis. I am transfering/transforming data and I don't know a damn thing about SSIS.
Ronnie Overby
OK, in that case, why don't you just do it with straight SQL then? Yes - Linq would be nice - but if it's a one-off - just drop to plain SQL and avoid all the problems and limitations :-)
marc_s
Either use straight SQL in your app, or go with SSIS - that's really SSIS's whole reason to be - extract-transform-load data from one source to another :-)
marc_s
A: 

Another option is to wrap all your Linq2Sql calls in a TransactionScope(). This should force them all to run in the same connection.

using System.Transactions; // Be sure to add a reference to System.Transactions.dll to your project.

       // ... in a method somewhere ...
       using (System.Transaction.TransactionScope trans = new TransactionScope())
       {
          using(YourDataContext context = new YourDataContext())
          {
             context.ExecuteCommand("SET IDENTITY_INSERT MyTable ON");

             context.ExecuteCommand("yourInsertCommand");

             context.ExecuteCommand("SET IDENTITY_INSERT MyTable OFF");
          }
          trans.Complete();
       }
       // ...

Although, if you are trying to do something like:

context.ExecuteCommand("SET IDENTITY_INSERT MyTable ON");
context.MyTable.InsertOnSubmit(myTableObject)
context.SubmitChanges()
context.ExecuteCommand("SET IDENTITY_INSERT MyTable OFF");

you will probably run into other issues, especially if the identity column has the IsDbGenerated attribute set to true. The SQL command generated by Linq2Sql will not know to include the identity column and value.

John Allers