How to save a child with assigned id in nhibernate

When cascading from a parent to a child, NHibernate uses the SaveOrUpdate method. You are correct that NHibernate need some way to determine whether it should perform an insert or an update. It will look at three different fields for an unsaved value to determine if the entity is new.

  1. Id
  2. Version
  3. Timestamp

With an assigned Id, you will need either a Version or Timestamp field in order to indicate that the entity is new.

An alternative would be to call Save() on the children explicitly.

Leave a Comment