NHibernate Pitfalls: Manually Assigned Identifiers
This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.
When you use manually assigned identifiers, you are responsible for assigning a valid id to your entity. By valid, I mean, some key that doesn’t already exist on the database.
Because it’s not NHibernate who is managing it, when time comes for the entity to be saved, NHibernate needs to issue a SELECT statement in order to find out if a row with the given id exists, in which case an UPDATE statement will be issued, otherwise, it will be an INSERT. So, you have to be very careful not no overwrite an existing row unintentionally.
Generally speaking, there’s no need to use manually assigned identifiers, and you should avoid them.