简体   繁体   中英

Updating the collections of entities with NHibernate the correct way

A simple question regarding 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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