且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NHibernate父母/子女关系

更新时间:2022-01-29 01:03:09

您应正常映射关系的两端,并且在将子级添加到父级集合中时,还应在子级上设置父级属性.通常,您可以通过编写如下方法来实现此目的:

You should map both sides of the relationship normally, and when you add a child to the parent's collection, you should also set the parent property on the child. Normally you would achieve this by writing a method like this:

public void AddChild(ChildEntity child)
{
   this.Children.Add(child);
   child.Parent = this;
}

NHibernate根据ChildEntity类中的映射属性在Child表中保留ParentId列.一对多关系的定义仅允许NHibernate根据此列中的值从数据库加载集合.

NHibernate persists the ParentId column in the Child table based on the mapped property in the ChildEntity class. The definition of the one-to-many relationship merely allows NHibernate to load the collection from the database based on values in this column