Fluent NHibernate 级联删除错误

发布于 2024-10-28 19:50:32 字数 634 浏览 3 评论 0原文

类似的问题已经被问过,但我没有找到答案,所以这里是。我映射了以下流畅的关系:

HasMany<UserFilter>(x => x.UserProjectFilters)
            .KeyColumns.Add("UserProfileID")
            .Cascade.All()
            .AsSet()
            .Inverse()
            .Cache.ReadWrite();

但是,当我尝试删除父级(过滤器实体)时,删除不会级联;我看到异常:“DELETE 语句与 REFERENCE 约束冲突......”。在 NH Profiler 中,我看到正在为父级生成删除语句,但没有为子级生成任何语句。我希望对任何子项的删除都会在父项之前执行。我做错了什么?

这是 UserProfileFilter 关系的结束:

References<Filter>(x => x.Filter)
            .Column("FilterID")
            .LazyLoad()
            .Cascade.SaveUpdate();

谢谢! 安迪

Similar questions have been asked, but I'm not finding an answer so here goes. I have the following Fluent relationship mapped:

HasMany<UserFilter>(x => x.UserProjectFilters)
            .KeyColumns.Add("UserProfileID")
            .Cascade.All()
            .AsSet()
            .Inverse()
            .Cache.ReadWrite();

When I try to delete a parent (Filter entity) though, the delete doesn't cascade; I'm seeing the exception: "The DELETE statement conflicted with the REFERENCE constraint ...". In NH Profiler, I see that the Delete statement is being generated for the parent, but none is generated for the child. I would expect the delete for any children to be executed prior to the parent. What am I doing wrong?

Here's the UserProfileFilter end of the relationship:

References<Filter>(x => x.Filter)
            .Column("FilterID")
            .LazyLoad()
            .Cascade.SaveUpdate();

Thank you!
Andy

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

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

发布评论

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

评论(2

傲鸠 2024-11-04 19:50:32

我认为您需要 Cascade.AllDeleteOrphan() 因为您已将关系设置为 Inverse()

I think you need Cascade.AllDeleteOrphan() as you've set up the relationship as Inverse()

明天过后 2024-11-04 19:50:32

这最终是子表上多个外键的问题,并且在映射中使用了错误的键。我将上面的代码更改为以下内容,效果很好:

HasMany<UserFilter>(x => x.UserProjectFilters)
            .KeyColumns.Add("FilterID")
            .Cascade.All()
            .AsSet()
            .Inverse()
            .Cache.ReadWrite();

谢谢大卫的帮助!

安迪

This ended up being a problem with multiple foreign keys on the child table, and the wrong key was being used in the mapping. I changed the code above to the following and it worked just fine:

HasMany<UserFilter>(x => x.UserProjectFilters)
            .KeyColumns.Add("FilterID")
            .Cascade.All()
            .AsSet()
            .Inverse()
            .Cache.ReadWrite();

Thank you for the help David!

Andy

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