I have an Entity that doesn’t override any of the equality members\operators.
When comparing two proxies of them (I got them from the Nhibernate session) the result changes according to the equality method:
- ReferenceEquals(first, second) – false.
- first == second – false
- Equals(first, second) – true.
This is even more weird as they both exist in the same session context and according to the Nhibernate docs:
NHibernate only guarantees identity ( a == b , the default
implementation of Equals()) inside a single ISession!`
And:
The instance is currently associated with a persistence context. It
has a persistent identity (primary key value) and, perhaps, a
corresponding row in the database. For a particular persistence
context, NHibernate guarantees that persistent identity is equivalent
to CLR identity (in-memory location of the object).
So why not all of the equality methods return true?
Update:
I get the enteties this way, Query the session for ChildEntity and get the Parents Entities with Linq’s select, similar to this:
var childs = session.Query<Child>();
var parents = childs.Select(x => x.ParentEntity).ToList();
After I get
childsfrom the session I Merge them with the session.It appear that the merge detached the entity from the session and return a new Proxy attached to the session.
It can be fixed with