views:

271

answers:

2

I have a database that uses natural keys (i.e. business oriented keys). Unfortunately, because of this, the primary keys for these objects may change over time.

I am currently researching the use of NHibernate for an O/RM for this database. However, in my testing I have noticed that there is no apparent way to change the primary key of an object and save it to the database.

For example, say I have a 'Business' object with a 'BusinessCode' as it's primary key:

public class Business
{
    public string BusinessCode { get; set; }

    public string Name { get; set; }

    ...
}

If I do a Get, change the primary key, and try and save it back to the database using NHibernate, I either receive an exception or unexpected results (depending on if I use Save(), Update(), or SaveOrUpdateCopy())

Business b = session.Get<Business>("BusinessCode1");

b.BusinessCode = "BusinessCode22";

session.Update( b );

So is something like this possible?

I understand that many NHibernate folks recommend using primary keys that do not change (i.e. identities). But I have a couple DB's that use natural keys. Thanks.

A: 

Have you tried using a composite-id?
Or Assigned Identifiers?

Is adding an Auto-incremented ID field totally out of the question?

RKitson
We actually are using composite-id's. So good call! I'm actually kicking myself that I even asked this question because I can easily mitigate this by doing a delete-insert type of operation. This was more of a proof-of-concept with an existing database. Thanks for your input!
Jeffaxe
A: 

I'm actually kicking myself that I even asked this question because I can easily mitigate this by doing a delete-insert type of operation. This was more of a proof-of-concept with an existing database. Thanks for your input!

Jeffaxe