Ronald
Ronald

Reputation: 2120

How to use sequence as default value for ID in NHibernate?

We have a table mapped in Fluent NHibernate with an Id generated by a Hilo sequence.

this.Id(x => x.Id).GeneratedBy.SeqHiLo("seq_hp", "1000");

We now need to specify a Default value on the Id (set to the next sequence value) to make it work with a SQL Merge statement.

However, NHibernate will generate the sequence only after the tables are generated, so the next statement will fail because there is not (yet) a sequence.

this.Id(x => x.Id).GeneratedBy.SeqHiLo("seq_hp", "1000")
                  .Default("next value for seq_hp");

Is there any way in NHibernate to specify the creation of the sequence before the creation of the table? We are using Fluent mapping, but NHibernate's code or XML mapping solutions are welcome.

Upvotes: 3

Views: 895

Answers (1)

Firo
Firo

Reputation: 30813

The other way around is also feasable. Implement IAuxiliaryDatabaseObject to add the Default for the column using Sql. There is the base class AbstractAuxiliaryDatabaseObject which does provide a default implementation.

Upvotes: 1

Related Questions