我是 NHibernate 的新手。我正在使用<设置...>一些多对一和多对多关联的映射。这些被公开为 ICollection 类型的属性,实际上由 HashSet 实现。
我的问题是,我是否应该重写相关类型的 Equals 和 GetHashCode ,以便它们与类型的数据库标识匹配(实际上,当 Id 属性相等时,对象相等)?或者 NHibernate 是否以某种方式为我处理这个问题?
如果这样做,如果我想在任意时间在多对多集合中插入多个值,就会出现问题,因为新元素可能具有 Guid.Empty 的 Id;因此被视为同一项目。
I am new to NHibernate. I am using <set ... > mapping for some many-to-one and many-to-many associations. These are exposed as properties of type ICollection<T>, in practice implemented by HashSet<T>.
My question is, should I override Equals and GetHashCode for the related types, so they match the database identity of the types (in practice so that the objects are equal when the Id property is equal) ? Or does NHibernate handle this for me in some way ?
If I do this, I see a problem if I want to insert multiple values in the many-to-many collection at any one time, because the new elements might have the Id of Guid.Empty; and therefore be considered the same item.
发布评论
评论(2)
您不一定会遇到这个问题,请查看 中的 rel="nofollow noreferrer">
AbstractEntity
(实现Equals
和GetHashCode
)You don't have to run into that problem necessarily, take a look at the
AbstractEntity
in uNhAddIns (which implementsEquals
andGetHashCode
)如果您要从两个不同的 Nhibernate 会话加载对象,请覆盖 Equals/GetHashCode。查看 Nhibernate 手册。
Override Equals/GetHashCode if you are loading objects from two different Nhibernate sessions. Check out the Nhibernate manual.