NHibernate – AutoGenerated Identity on a Non-Key Field

We have a requirement to store an additional auto-incremented id to support a legacy app upgrade.  Our primary key is a GUID – the old system used ints, and the client still wants to have that int id – as they use it, etc…

So, within NHibernate we were able to add the following to the property:

property name="GroupId" type="int" generated="always" insert="false" />
 
Thats it.
 
 
Advertisements

2 thoughts on “NHibernate – AutoGenerated Identity on a Non-Key Field

  1. Hi .. this works great but the instance of the class that I am saving doesn’t get the auto-incremented value after I do a save. Do I have disgard the saved instance and reload it in order for the property to be set in the class instance? Thanks.

  2. I did not have to reload it.

    I am calling ‘Session.Save(entity);’ on the object in my ‘repository’ and returning the object back.

    ie.

    public T Create(T entity)
    {
    var valMgr = new ValidationManager(entity);
    if (valMgr.IsValid())
    {
    Session.Save(entity);
    }
    else
    {
    throw new ValidationException(string.Format(“Validation failed for {0} : {1}”, entity.GetType().Name, valMgr.ValidationErrorsToString));
    }
    return entity;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s