views:

239

answers:

1

As the question states, is it possible to map a composite key in fluent nhibernate (or non fluent i suppose) where one of the two fields used in the composite is an identity field?

I have a table where one part of the primary key is an identity field and the other is a tenant id. This is a legacy database where the composite key is used as foreign keys all over, so modifying the database would be quite significant.

thanks!

A: 

If it's really an identity column, then it's guaranteed to be unique for every record, right?

If so, you can ignore the tenancy ID completely for mapping purposes. Your identity fields will be unique in your primary table - AND your foreign key references will contain this unique Id, so the tenancy ID is actually irrelevant.

NHibernate doesn't really care whether you're mapping your database schema exactly. I've worked with legacy databases before where I've defined a unique constraint on a non-key column and then told NHibernate to use that as the primary key - it works, and it can neatly sidestep all kinds of horrific composite-key relationship mappings.

Think about it. Counter-intuitive, but it will probably work.

Dylan Beattie