Does nhibernate proxies do any smart job to make change tracking efficient? Or does it only support what Entity Framework calls snapshot based change tracking?
-
Are you talking about audit logging, where changes are recorded, or full change management with the ability to revert to earlier versions of entities? – DanK May 28 '10 at 08:02
-
If entity has several properties ORM should issue update statement to the database with only changed properties. In LINQ TO SQL, it knows that properties have changed or not through INotifyPorpertyChanging/Changed interface(though it's not a requirement, in which case it falls back to snapshot based change tracking, that is it compares every properties to find out which properies have changed). – Alice May 28 '10 at 15:33
2 Answers
It is snapshot-based.
When loading an entity, its state is stored in the session as an object[].
When flushing, the current state is converted to an object[] and compared with the original state to determine which properties are dirty.
This is more efficient for many reasons. One of them is that you don't need a proxy to track changes. Another is that, if you set a property to a different value and then revert it, the entity will be considered not-dirty, thus avoiding an unnecessary DB call.

- 52,548
- 16
- 116
- 154
-
Why NHibernate does not support updating only modified fields? If snapshot is already stored, it can easily identify the modified fields and should update only those. IMO, it always update all the fields. Can you please explain? – Amit Joshi Aug 13 '19 at 13:25
-
2@AmitJoshi it does support it; it's just not the default behavior. You need to set `dynamic-update="true"` in the entity mapping. – Diego Mijelshon Aug 13 '19 at 13:28
-
Any reason why it is not a default behavior? Actually, by updating specific fields by default will generate more performance efficient query. Is it because it costs more to track each property while `dynamic-update` is `true`? – Amit Joshi Aug 13 '19 at 13:44
-
1@AmitJoshi It's not always more efficient, because the `update` statement has to be generated for every change, instead of once in the app lifetime. Also, you might want your changes to override an entire object instead of mixing your changes with somebody else's. In general, the default behavior is more predictable, but you can change it. – Diego Mijelshon Aug 13 '19 at 13:49
NHibernate and EntityFramework track changes in very different ways. Entity Framework tracks changes in the entity itself. NHibernate tracks changes in the session.
Tracking changes in the entity requires more memory (because you are storing the before values as well as the after values). Entities can retain change tracking even after disconnecting from the ObjectContext.
Tracking changes in the session is more efficient overall, but if you disconnect an entity from the session, you lose the change tracking.

- 12,331
- 5
- 38
- 40
-
3Is this statement correct? As far as I know, EF uses a so called change tracker to track entity changes. No change tracking infrastructure is contained in the entity. – Andreas Feb 29 '16 at 08:03