-1

I'm writing an N-tier application that uses NHibernate and a Repository pattern to interface with a SQL store. Because the project is N-Tier, the NHibernate entities are mapped to the domain model who are then mapped to view models, and vice versa, causing models to be disconnected between NHB sessions.

The problem I have is that when I attempt to save a changed entity (an entity that has been recently instantiated to represent an old, changed entity from a previous session) using the Merge() method, I get an exception about violating a unique index on my table because NHB is trying to insert instead of update a row with the same ID as the entity. I've double checked my mappings, and that my entity contains all the right values in all the right places... it would seem NHB is not checking if a row exists before trying to insert, thoughts?

4

1 回答 1

0

想通了问题。导致此问题的对象图包含与子对象引用其父对象的一对多关系,并且我的映射器正在为每个子对象对父对象的引用实例化相同“实体”的新实例。这意味着 ChildA 和 ChildB 的“Parent”属性持有的实例不相同,但包含相同的信息,不知何故导致 NHB 想要插入新记录而不是根据 PK 做出决定。我发现这种行为很奇怪,因为我重写了 Equals/GetHashCode 来执行基于值的相等,但我很高兴解决了这个问题。

于 2013-06-03T04:42:23.910 回答