Fluent NHibernate级联删除问题

发布于 2024-10-05 08:33:01 字数 863 浏览 2 评论 0原文

我在流畅的 nhibernate 级联删除方面遇到问题。我确信我做错了什么,因为它不起作用。

这是我的对象:

public class Parent
{
    public int Id { get; set; }
    public IList<SequencedChild> SequencedChildren { get; set; }
}

public class SequencedChild 
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public int ChildId { get; set; }
    public int Sequence { get; set; }
}

public class Child 
{
    public int Id { get; set; }
}

这是我的映射:

HasMany(m => m.SequencedChildren).Inverse().Cascade.Delete();

所以我有一个父级,有一些已排序的子级,我想更新该父级以使其没有子级。当我在父级上没有排序子级的情况下进行更新时,我希望在我的 SequencedChild 表中,具有父级 id 的记录将被删除。但由于某种原因,NHibernate 试图用 null 更新这些记录的 ParentId - 由于 ParentId 不为 null,因此失败。 编辑:我还希望 Child 对象不受影响(其行为正确)。

我看了几个问题,他们都建议使用逆,但我已经在这样做了。我做错了什么?

I'm having a problem with fluent nhibernate cascade delete. I'm sure I'm doing something wrong because it isn't working.

Here are my objects:

public class Parent
{
    public int Id { get; set; }
    public IList<SequencedChild> SequencedChildren { get; set; }
}

public class SequencedChild 
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public int ChildId { get; set; }
    public int Sequence { get; set; }
}

public class Child 
{
    public int Id { get; set; }
}

And here is my mapping:

HasMany(m => m.SequencedChildren).Inverse().Cascade.Delete();

So I have a parent with some sequenced children and I want to update that parent to have no children. When I do an update with no sequenced children on that parent I expect that in my SequencedChild table the records that have the id of the parent will be deleted. But for some reason NHibernate is trying to update the ParentId of those records with null - which fails as ParentId is not null. EDIT: I'm also expecting that the Child object is unaffected (which is behaving correctly).

I had a look at a few questions and they all suggest the use of inverse but I am already doing this. What am I doing wrong?

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

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

发布评论

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

评论(2

冷清清 2024-10-12 08:33:01

因此,我设法找到了一个解决方案,结果有多个步骤:

  1. 正如 James 在评论中指出的那样,ParentId/ChildId 应该是 Parent/Child 引用,而不仅仅是 ID(为此+1)
  2. SequencedChild 需要拥有将级联设置为 none 的显式映射
  3. 进行更新时,不要覆盖 SequencedChild 列表。首先清除它然后添加新项目。 (或者只是清楚,如果您不替换项目)
  4. Inverse() 调用不是必需的
  5. db 表中的 ParentId 字段应该可以为空,因为 nHibernate 坚持将其更新为null 在删除之前。 (如果有人知道解决方法请发表评论)

So I managed to find a solution which turned out to have multiple steps:

  1. As James pointed out in a comment ParentId/ChildId should be Parent/Child references not just the ids (+1 for that)
  2. SequencedChild needs to have an explicit map that sets cascade to none
  3. When doing the update, don't overwrite the SequencedChild list. First clear it then add the new items. (Or just clear if you arent replacing the items)
  4. The Inverse() call is not necessary
  5. The ParentId field in the db table should be nullable because nHibernate insists on updating it to null before it deletes it. (if anyone knows a way around this leave a comment)
黒涩兲箜 2024-10-12 08:33:01

尝试将级联更改为 Cascade.AllDeleteOrphan() 以删除 SequencedChild 表上的孤立子记录。

Try changing the cascade to Cascade.AllDeleteOrphan() to remove orphaned child records on the SequencedChild table.

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