I'm running an NHIbernate solution using SQL CE. I am mapping one the fields in a table as below. However, in order to run some data imports I need to be able to temporarily turn off identity so I can import the data with its existing keys, then turn identity back on once the import has finished.
I've tried running a SQL query directly from the solution like this:
session.CreateSQLQuery(@"SET IDENTITY_INSERT [Article] ON");
but this seems to have no effect.
Is there any way to temporarily turn this on and off?
Property(x => x.ArticleId, m =>
{
m.NotNullable(true);
m.UniqueKey("UQ_Article_ArticleId");
m.Column(cm => cm.SqlType("INT IDENTITY"));
m.Generated(PropertyGeneration.Insert);
m.Insert(true);
m.Update(false);
});
It was something fairly simple in the end.
The sql line should have been this:
session.CreateSQLQuery(@"SET IDENTITY_INSERT [Article] ON").ExecuteUpdate();
and it needed to be inside a transaction. However, you can only do this on one table per transaction so didn't really help me that much.