I have been using the generator class assigned
to assign my primary key values. I have read several blogs which state that hilo
is better. Is there a benefit to assigned
over hilo
?
views:
33answers:
1assigned
is just useful when you want to define the primary key yourself.
If you get the value from somewhere, for instance from another database or an imported file, assigned
could be useful. But then it's most certainly a guid, not an integer. You may also use natural primary keys, which are business data and primary key at the same time. Although it is discouraged to do this.
I would say, assigned
is for special cases, not for regular applications. Usually you use artificial keys, so you don't care about the exact value. Generating the value yourself could be difficult, at least when you consider several processes using the same database.
There is a major difference between assigned
and (most?) other id generators. assigned
keys are not given by NHibernate nor by the database. NHibernate usually knows if an entity is already in the database by evaluating the id. When you assign it yourself, you also need to know yourself if the entity needs to be inserted or updated. This make this id generator special. Your code will look different when you use assigned
as when you use any other id generator.