Linq To Sql and identity_insert

后端 未结 6 1011
星月不相逢
星月不相逢 2021-01-13 04:29

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

I have tried calling
mycontext.ExecuteCommand("S

6条回答
  •  醉梦人生
    2021-01-13 05:18

    It should be enough to open the connection manually before executing commands. This makes the commands run in the same session:

    context.Connection.Open();
    context.ExecuteCommand("SET IDENTITY_INSERT MyTable ON");
    // make changes
    // ...
    context.SubmitChanges();
    

提交回复
热议问题