hello!
i have a table "Fund" where the primary key is identity (autoassigned). now in a certain case i need to insert a record with a given Id. NHibernate's session.save(obj, id) would in theory allow this. however, as expected, the DBS (MSSQL2000) does not allow that.
so my question is: how can i insert a record with a predefined id?
one solution could be to use an ADO session, submit a IDENTITY_INSERT ON and OFF before and after the NHibernate session.save(obj, id) and somehow "bind" that session to the ADO session. this is where i can't figure it out. here's a part of my code (removed unimportant parts):
Code:
Protected Function addWithGivenPK(ByVal fund As FundDTO, ByVal id As Long) As FundDTO Implements IFundDAO.addWithGivenPK
   'Dim tx As NHibernate.ITransaction
   Dim tx2 As SqlTransaction
   Dim conn As SqlClient.SqlConnection = New SqlConnection(myConnectionString)
   Dim ret As Integer
   Dim cmdIdentityInsertOn As SqlClient.SqlCommand = New SqlClient.SqlCommand("SET IDENTITY_INSERT Fund ON; ", conn)
   'Dim cmdMiddle As SqlClient.SqlCommand = New SqlClient.SqlCommand("INSERT INTO Fund (FundId, Name) VALUES (101, 'test'); ", conn)
   Dim cmdIdentityInsertOff As IDbCommand = New SqlClient.SqlCommand("SET IDENTITY_INSERT Fund OFF", conn)
   conn.Open()
   Dim session2 As ISession = getSession(conn)
   tx2 = conn.BeginTransaction
   cmdIdentityInsertOn.Transaction = tx2
   'cmdMiddle.Transaction = tx2
   cmdIdentityInsertOff.Transaction = tx2
   ret = cmdIdentityInsertOn.ExecuteNonQuery()
   session2.Save(fund, id)
   'ret = cmdMiddle.ExecuteNonQuery()
   ret = cmdIdentityInsertOff.ExecuteNonQuery()
   tx2.Commit()
   conn.Close()
   Return fund
[ ... ]
please note: the cmdMiddle part was used to test if the id-insert command worked and it did works.
please note 2: no exception thrown here...
any hints on how to get the nhibernate use the same session or maybe even a better (nhibernate-integrated) solution?