nHibernate one-to-many inserts but doesnt update

筅森魡賤 提交于 2020-01-15 09:09:33

问题


Instead of getting into code, I have a simple question. 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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1732708/nhibernate-one-to-many-inserts-but-doesnt-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!