Fluent NHibernate 级联删除错误
类似的问题已经被问过,但我没有找到答案,所以这里是。我映射了以下流畅的关系:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要
Cascade.AllDeleteOrphan()
因为您已将关系设置为Inverse()
I think you need
Cascade.AllDeleteOrphan()
as you've set up the relationship asInverse()
这最终是子表上多个外键的问题,并且在映射中使用了错误的键。我将上面的代码更改为以下内容,效果很好:
谢谢大卫的帮助!
安迪
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:
Thank you for the help David!
Andy