开发者

Nhibernate Merge with new object graph No row with the given identifier exists

开发者 https://www.devze.com 2023-02-04 20:34 出处:网络
If i create a new Parent object and then add new child object to it and attempt to do ISession.Merge(parent) I get \"No row with the given identifier exists\" exception for child object.

If i create a new Parent object and then add new child object to it and attempt to do ISession.Merge(parent) I get "No row with the given identifier exists" exception for child object.

Isn't Merge suppose to create a persistent object if it's not found in DB? It only creates it if I merge the parent with an empty child collection.

Maybe something is wrong in my mappings?

I have this at child:

<many-to-one class="Parent fetch="join" name="Parent">
  <column name="ParentId" />
</many-to-one>

And this at parent:

<bag fetch="join" inverse="true" lazy="false" name="Childs" mutable="true">
  <key>
    <column name="ParentId" />
  </key>
  <one-to-many class="Ch开发者_如何学Cild" />
</bag>


You may use cascade or merge the child separately:

<bag fetch="join" inverse="true" lazy="false" name="Childs" cascade="all">
  ...
</bag>

Merging the children separately:

foreach(Child child in parent.Children)
{
  session.Merge(child);
}

var mergedParent = session.Merge(parent);

I'm actually not sure what happens if the children are already in the session cache but the parent is not. You probably need to build an instance with all the merged data yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号