The solution I propose involves quite a bit of code, but you can just copy it all and past it in a VS test solution assuming you have SqLite installed, and you should be able to
I think the basic misconception here is that you implement Equals and GetHashCode based on business data. I don't know why you prefer that, I can't see any advantage in it. Except - of course - when dealing with a value object which doesn't have an Id.
There is a great post on nhforge.org about Identity Field, Equality and Hash Code
Edit: This part of your code will cause problems:
public static class IdentityChanger
{
public static void ChangeIdentity(Action changeIdentity, T newIdentity, ISession session)
{
changeIdentity.Invoke(newIdentity);
session.Flush();
session.Clear();
}
}
You should implement Equals
and GetHashCode
based on immutable data. Changing the hash is not possible in a reasonable way.