开发者

nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple quest开发者_开发知识库ion. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key.

Has anyone ever had a one-to-many where the child object gets inserted but not updated resulting in a row in my table with a null in the foreign key column?

I want the default behaviour for a standard one-to-many. I don't want to have to add the parent as a property to the child.

Thanks.


This would happen if you didn't have cascade="save-update" on your set/bag

or if you set your session's FlushMode to 'None' or 'Commit' and saved the child using your childRepository and neglected to save the object containing the collection using its repository.


I think you have to set parent reference in child item.

class Parent {
  public virtual IList<Child> Children;
}

class Child {
  public virtual Parent Parent;
}

Parent p = new Parent();
Child c = new Child();
c.Parent = p;
p.Children = new List<Child>();
p.Children.Add(c);

Now when you save this transient object p you will have the right foreign key in the child table.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜