开发者

Updating the collections of entities with NHibernate the correct way

开发者 https://www.devze.com 2022-12-15 17:30 出处:网络
A simple question re开发者_StackOverflowgarding how NHibernate works: I have a parent entity that has a collection of other child entities. Those child entities have a reference to the parent entity t

A simple question re开发者_StackOverflowgarding how NHibernate works:

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. 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? Do I have to do all these things in that method or will NHibernate do something for me?

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). The last step is the only one for which I'm not sure it's mandatory.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号