sql-serverstored-proceduresnhibernatenhibernate-mappingfiletable

How to map newsequentialid with NHibernate


I am using SQL Server FileTable with NHibernate.

On insert, NHibernate will call a stored procedure that inserts a record into the FileTable. The problem I have is that the Id (stream_id) column in the FileTable is generated by SQL Server on insert. It is generated by the default constraint newsequentialid().

How can I return that id from my stored procedure and map it to id property of my entity?

Update:

Regarding the other question: [NHibernate use stored procedure OR mapping][1] This is not applicable here. I am using a stored procedure to insert an entity. That stored procedure must return an Id of type Guid that the database has generated at insert time. I want NHibernate to map this id to the entity.

I need something like Generators.TriggerIdentity but for Guid instead of Int.

Update2:

I managed to populate my Entity.Id by setting it's Id Generating strategy to 'Native' and selecting a return value from the stored procedure. But this breaks the transaction because to get the Id it needs to go to the database and do an insert.


Solution

  • I managed to resolve this by changing the Id Generator to Generators.Native. Then i had to return the inserted id from the stored procedure.