NHibernate: Reasons for overriding Equals and GetHashCode

前端 未结 2 1429
小鲜肉
小鲜肉 2020-12-01 16:37

Are there any reasons why Equals or GetHashCode should be overriden in entities when using NHibernate? And in which scenarios are these reasons valid?

Some reasons t

2条回答
  •  情话喂你
    2020-12-01 16:58

    Overloading the Equals and GetHashCode methods is important if you are working with multiple sessions, detached entities, stateless sessions or collections (see Sixto Saez's answer for an example!).

    In the same session scope identity map will ensure that you only have a single instance of the same entity. However, there is the possibility of comparing an entity with a proxy of the same entity (see below).

提交回复
热议问题