在表 yy 上引入 FOREIGN KEY 约束 xx 可能会导致循环或多级联路径
我已阅读这个答案,但是,它是超过 2 岁了,我希望可能有一个专门针对实体框架的答案。
为了快速解释,我有一个可以引用自身的类型,我已经一遍又一遍地检查了架构,并且我确信这是实现它的最佳方法
...... sstatic.net/GfIu2.png" alt="在此处输入图像描述">
它永远不会将自身作为参考,它始终是一个新的参考,因此,此错误不应成为问题。
话虽这么说,我完全理解,从技术上讲,在 SQL 中它可以引用自身,但是,它永远不会(从代码中)。
禁用级联删除允许它工作,但是,我真的不想禁用它,因为它是一个复杂的程序,并且我利用链中“更高”的级联删除。
I have read this answer, however, it is over 2 years old and I was hoping that there may be an answer dealing specifically with Entity Framework.
To explain quickly, I have a type that can refer to itself, I have been over and over the schema, and I am certain that this is the best way to achieve it...
It will never have itself as a reference, it would always be a new one, so, this error should not be an issue.
That being said, I completely understand that technically in SQL it can refer to itself, however, it never will (from the code).
Disabling cascade delete allows this to work, however, I really do not want to disable this as it is a complicated program and I take advantage of Cascade Delete "higher up" in the chain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 SQL 抱怨多个级联路径,则没有什么可做的,您根本无法在数据库中使用级联删除来处理这种情况,但您可以使用解决方法 - 而不是使用内置级联删除,使用
INSTEAD OF DELETE
触发器并编写自定义 SQL 以先删除从属记录,然后再删除主记录。在EF中尝试使用级联删除,因为这种情况下数据库层对EF应该是透明的。If SQL complains about multiple cascade paths there is not much to do you simply cannot use cascade delete in database for such case but you can use workaround - instead of using built-in cascade delete use
INSTEAD OF DELETE
trigger and write custom SQL to delete dependent record first and after that delete the principal record. In EF try to use cascade delete because the database layer should be transparent to EF in this case.