NHibernate父母/子女关系

编程入门 行业动态 更新时间:2024-10-15 02:31:03
本文介绍了NHibernate父母/子女关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下设置:

public class ParentEntity { public ICollection<ChildEntity> {get; set; } } public class ChildEntity { // do i need to the parent here? }

我设法保存了ParentEntity并将保存级联到添加的子实体,这些子实体也已保存.但是在db表中,将子代的ParentId引用设置为允许NULL.将其设置为NOT NULL时,保存失败,因为子表中的ParentId为NULL.

I managed to save the ParentEntity and cascaded the save to the added child entities which were saved as well. But in the db table the ParentId reference of the child was set to allow NULL. When setting it to NOT NULL the save failes since the ParentId in the child table is NULL.

那里发生了什么? ;)

What's happening there? ;)

何时

推荐答案

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

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

更多推荐

NHibernate父母/子女关系

本文发布于:2023-10-28 03:59:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535449.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子女   父母   关系   NHibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!