简体   繁体   English

使用NHibernate更新实体集合的正确方法

[英]Updating the collections of entities with NHibernate the correct way

A simple question regarding how NHibernate works: 关于NHibernate如何工作的一个简单问题:
I have a parent entity that has a collection of other child entities. 我有一个具有其他子实体集合的父实体。 Those child entities have a reference to the parent entity they belong to. 这些子实体引用了它们所属的父实体。 Now I want to implement an Add method to the parent entity that would add a child to it. 现在,我想对父实体实现一个Add方法,该方法将为其添加子项。 Should that Add method only add the child to its new parents collection, or should it also update the parent reference of the child or should it also remove the added entity from its previous parents collection? 该Add方法是仅将子级添加到其新的父级集合中,还是应该更新子级的父级引用,还是应从其先前的父级集合中删除添加的实体? Do I have to do all these things in that method or will NHibernate do something for me? 我必须使用该方法执行所有这些操作,还是NHibernate为我做些事情?

Thanks. 谢谢。

You should definitely make sure that the relationship is "valid" for all parties involved. 您绝对应该确保关系对所有参与方都是“有效的”。

Make sure that the parent has the child in its children collection, and the the child has the parent in its parent reference, and that the child is removed from its old parent (if it had one). 确保父项在其children集合中具有children ,并且子项在parent引用中具有parent ,并确保将子项从其旧parent移除(如果有)。 The last step is the only one for which I'm not sure it's mandatory. 最后一步是我不确定是否必须执行的唯一步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM