5

I have a application that uses C# and NHibernate and it will support SQL Server 2008, SQL Server 2012 and Oracle. I have been using fluent nhibernate to map my entities and I have some questions about how should I map the ID. Oracle supports only the Sequence, SQL Server 2008 only identity and Sql Server 2012 both. I would like to map in Sql Server (2008 and 2012) with Identity and Oracle with Sequence on the same code.

How should I map the ID to work for all databases?

It does not matter if I will have some IF's statement on my fluent mapping code. Looks my codes for mapping:

For SQL:

Id(x => x.Id).GeneratedBy.Native();

For Oracle:

Id(x => x.Id).GeneratedBy.Sequence("SQ_Customer");

PS: I do not want any workaround to achieve it. I want a NHibernate/Fluent-NHibernate solution to map it.

2
  • Do you have to wait for database to give you the id, or can you generate your own using nhibernates HILO generator? Commented Jun 20, 2013 at 15:48
  • I was think using a Guid but unfortunately I can not change my database :( Commented Jun 20, 2013 at 16:52

1 Answer 1

8
+100

Well, "Native" chooses Identity, sequence or HILO automatically depending on the used DB.

Using this xml mapping it should use identity or the sequence sq_customer if its Oracle:

  <generator class="native" >
    <param name="sequence">sq_customer</param>
  </generator>

This post describes things further.

Sign up to request clarification or add additional context in comments.

4 Comments

It's the correct answer. Translated to Fluent: Id(x => x.Id).GeneratedBy.Native("SQ_Customer");
Id(x => x.Id).GeneratedBy.Increment() works without needing a sequence.
I was searching 2 days for solution in Java. No answers like that for Java. No info in doc. This "trick" helped me very much, worked perfectly for MS SQL Identity and Oracle SEQUENCE-TRIGGER. Thank you very much.
great I was of assistance, vote up the answer if you don't mind :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.