从父集合中删除子记录

发布于 2024-07-23 15:34:57 字数 545 浏览 6 评论 0原文

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

这是我的映射

篮:

<键列=“篮子Id”/> <一对多类=“BasketItem”/>

BasketItem:

我想打电话basket.RemoveBasketItem(BasketItem item) 然后 Session.SaveUpdate(basket) 以便删除购物篮项目。 这可能吗?

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.

Here are my mappings

Basket:

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

BasketItem:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哀由 2024-07-30 15:34:57

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

cascade="all" 仅当父记录被删除时才会删除您的子记录。

Change cascade="all" into cascade="all-delete-orphan".

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

暮年 2024-07-30 15:34:57

我有相同的场景,并且在 bagList 中使用了cascade=“all-delete-orphan”,但是当我删除集合中的单个子项时,它也会删除父对象。

I have same scenario and ive used cascade="all-delete-orphan" in bagList but when i delete a single child item in a collection it deletes the parent object as well.

心凉怎暖 2024-07-30 15:34:57

由于从我的集合 getter 返回一个只读的新列表,我遇到了与 initforthemoney 相同的问题。 我发现我可以通过将集合的属性访问策略从 nosetter 更改为 field 来继续使用 ReadOnly 列表。

I was having the same problem as initforthemoney due to returning a new list as ReadOnly from my collection getter. I found I could continue to use the ReadOnly list by changing the property access strategy of the collection from nosetter to field.

心舞飞扬 2024-07-30 15:34:57

我遇到了一个问题,我的孩子元素返回有序枚举。

private readonly IList<Child> children;

public virtual IEnumerable<Child> Children { get { return children.OrderBy(c => c.Position); } }

public virtual void DeleteChild(Child item)
{
    children.Remove(item);
}

我将顺序移至映射并返回子项,就像 IEnumerable 一样。 这对我有用!

I was having a problem where my children elements where returning an Ordered enumerable.

private readonly IList<Child> children;

public virtual IEnumerable<Child> Children { get { return children.OrderBy(c => c.Position); } }

public virtual void DeleteChild(Child item)
{
    children.Remove(item);
}

I moved the ordering to my mapping and returned the children as it is for the IEnumerable. This worked for me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文