In your example code, you call Update
on an entity. This if for updating detached entities, entities which are not tracked by your current NHibernate session.
When updating detached entity, you tell NHibernate to take the supplied entity and consider it as being the complete new state of it, for updating it in database. So all the properties you have not set will have their default values, and NHibernate will consider it has to update the database with those default values.
If you want to only change one property, you must first load your entity with NHibernate, change the property, then Flush
the session.
var r = session.Load<Rezervace>(id);
r.Stav = 1;
session.Flush();
There are no need to tell to NHibernate which entity are you updating when the entity was loaded from the current session.
By default, NHibernate will still update all properties with their previous values and the changed one with its new value. As said by Rabban answer, you have to enable dynamic-update
on your class mapping for changing this behavior, and have NHibernate update only the changed property.
Notes:
I now consider I should have not answered but:
- Flag the question for closing, either as unclear or duplicate (now done as unclear).
- Point toward current possible answers in already existing questions.
This answer to one of those other questions is better than mine by the way, if we are in the detached case.
Dynamic-Update
to true in your mappings. But you have to set it for every class where you want this behaviour.