从父集合中删除子记录 [英] Delete a child record from the parent collection

查看:73
本文介绍了从父集合中删除子记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个示例应用程序,以便我可以了解NHibernate的来龙去脉.我正在努力解决删除问题.我希望能够通过从子记录的父集合中删除子记录然后保存父记录来删除子记录.我已经建立了双向一对多关系,并且插入/更新效果很好.

I'm developing a sample application so that I can learn the ins and outs of NHibernate. I am struggling with a delete issue. I wish to be able to delete a child record by removing it from its parent’s collection and then saving the parent. I have setup a bidirectional one-to-many relationship and inserting/updating is working great.

这是我的映射

购物篮:

<bag name="Items" inverse="true" cascade="all"> <key column="BasketId" /> <one-to-many class="BasketItem" /> </bag>

<bag name="Items" inverse="true" cascade="all"> <key column="BasketId" /> <one-to-many class="BasketItem" /> </bag>

BasketItem:

BasketItem:

<many-to-one not-null="true" name="Basket" column="BasketId" />

我想先呼叫basket.RemoveBasketItem(BasketItem item)然后呼叫Session.SaveUpdate(basket),以便删除购物篮项目.这可能吗?

I would like to call basket.RemoveBasketItem(BasketItem item) then Session.SaveUpdate(basket) so that the basket item will be deleted. Is this possible?

推荐答案

cascade="all"更改为cascade="all-delete-orphan".

cascade="all"仅在删除父记录后才删除您的子记录.

cascade="all" will only delete your child records if the parent gets deleted.

这篇关于从父集合中删除子记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆