NHibernate uses the primary key value of your class in its internal state-tracking engine to identify that instance.
When you have a single primary key property in your domain, NHibernate will use the value returned by the property as the key value, calling GetHashcode
and Equals
on the value, using the primary key much like you would use it in a Dictionary<TKey,TValue>
instance.
When there are multiple primary key properties forming a composite key, NHibernate has no trivial way to get a key value it can use. It requires you define how to determine equality between two instances, effectively turning the instance into its own key much like it would be in a HashSet<T>
.
If you haven't overridden Equals
and GetHashCode
on your type to use your composite key values, it does not reflect the "equality" used by the data-model (two rows being equal if they have the same primary key) and NHibernate cannot be certain it is tracking the correct entities; this is why the exception occurs.
You can find out an overview of composite keys and more information on clever ways to deal with them here.