本文介绍了nHibernate一对多插入但不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题,而不是进入代码.简单的一对多的默认行为是:它插入子记录,然后用父键更新外键列.

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.

谢谢.

推荐答案

我认为您必须在子项中设置父引用.

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);

现在,当您保存此瞬态对象p时,您将在子表中具有正确的外键.

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

这篇关于nHibernate一对多插入但不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:56