NHibernate HiLo generation and SQL 2005/8 Schemas

Posted by Kirk Clawson on Stack Overflow See other posts from Stack Overflow or by Kirk Clawson
Published on 2010-06-11T15:07:08Z Indexed on 2010/06/11 17:42 UTC
Read the original article Hit count: 498

Filed under:
|
|

I have an issue on my hands that I've spent several days searching for an answer to no avail...

We're using HiLo Id generation, and everything seems to be working fine, as long as the entity table is in the same schema as the hibernate_unique_key table.

The table structure is pretty simple. I have my hi value table in the db as dbo.hibernate_unique_key. Several entity table are also in the dbo schema, and they work without issue. Then we have tables under the "Contact" schema (such as Contact.Person and Contact.Address).

In the Person Mapping file:

<class name="Person" table="Person" schema="Contact">
<id name="Id" unsaved-value="0">
  <generator class="hilo">
    <param name="max_lo">100</param>
  </generator>
</id>
...

When I try to insert a Person entity, I get an error of "Invalid object name 'Contact.hibernate_unique_key'. That error is certainly clear enough. So I add:

<param name="schema">dbo</param>

to my mapping file/generator element. Now, when the SessionFactory is built, I get a "An item with the same key has already been added." error. So now I'm a bit stuck. I can't leave the HiLo generator without a schema, because it picks up the schema from the Class, and I can't specify the schema because it's already been added (presumably because it's my "default_schema" as identified in my XML cfg file).

Am I completely hosed here? Must I either

A) Keep all my tables in the dbo schema or

B) Create a separate HiLo Key table for each unique schema in the DB?

Neither of those scenarios is particularly palatable for my application, so I'm hoping that I can "fix" my mapping files to address this issue.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about schema