简体   繁体   English

级联删除在 EF Core 3 中似乎对我不起作用

[英]Cascade Deletion doesn't seem to work for me in EF Core 3

I have an Entity Framework hybrid project with several related tables.我有一个包含多个相关表的实体框架混合项目。

I have created the Foreign Keys with Cascades on Delete where appropriate both on the database and in the dbcontext but when I run a delete call to the API the related data gets orphaned instead of deleted.我已经在数据库和 dbcontext 中的适当位置创建了具有级联删除的外键,但是当我对 API 运行删除调用时,相关数据将被孤立而不是删除。

Everything I have read the last couple of days indicates that this should work.我在过去几天阅读的所有内容都表明这应该有效。 I much prefer the database to handle this workload instead of doing them manually in code.我更喜欢数据库来处理这种工作负载,而不是在代码中手动执行它们。

My Foreign Key naming conventions match what is in my dbcontext as does the logic, near as I can tell.据我所知,我的外键命名约定与我的 dbcontext 中的逻辑相匹配。 The basic structure is that this API stores configurations for running reports.基本结构是这个 API 存储运行报告的配置。 A Report as 1+ Stored Procedure and which have n Parameters.报告为 1+ 存储过程,并且有 n 个参数。 A Stored Procedure record should not exist without a connected Report and each Parameter should likewise not exist without a connected Stored Procedure.如果没有连接的报告,则不应存在存储过程记录,并且如果没有连接的存储过程,每个参数同样不应存在。

Below is part of the Fluent API for Stored Procedures (Sprocs).以下是用于存储过程 (Sprocs) 的 Fluent API 的一部分。

        entity.HasOne(d => d.Report)
            .WithMany(p => p.Sprocs)
            .HasForeignKey(d => d.ReportId)
            .IsRequired()
            .OnDelete(DeleteBehavior.Cascade)
            .HasConstraintName("FK_Sprocs_Reports");

        entity.HasOne(d => d.ServerInfo)
            .WithMany(p => p.Sprocs)
            .HasForeignKey(d => d.ServerInfoId)
            .IsRequired()
            .HasConstraintName("FK_Sprocs_UsableSchema");

The scripted Foreign Key from SQL Server is as follows: SQL 服务器的脚本外键如下:

ALTER TABLE [dbo].[Sprocs]  WITH NOCHECK ADD  CONSTRAINT [FK_Sprocs_Reports] FOREIGN KEY([ReportId])
REFERENCES [dbo].[Reports] ([ID])
ON DELETE CASCADE
GO

We did start this project in Core 2.2 and later upgraded during the project cycle to 3.0 and I realize there were some breaking changes to Cascades but my understanding (which may be flawed) was that the order of operations was the only thing that changed for OnDelete behavior.我们确实在 Core 2.2 中启动了这个项目,后来在项目周期中升级到 3.0,我意识到 Cascades 有一些重大变化,但我的理解(可能有缺陷)是操作顺序是 OnDelete 唯一改变的事情行为。

None of the cascades are working although I only have a few.尽管我只有几个级联,但没有一个级联在工作。 I have User => UserGroupXREF so if I delete a given User I need to remove its proprietary Group as well as the relationship mapping in the XREF table.我有 User => UserGroupXREF 所以如果我删除给定的用户,我需要删除其专有组以及 XREF 表中的关系映射。 I am pretty sure that will be two calls/steps.我很确定这将是两个电话/步骤。

I can't seem to find any indication that I have this set up incorrectly but my Google-Fu has failed me before on word choice for searches.我似乎找不到任何迹象表明我的设置不正确,但我的 Google-Fu 在搜索单词选择之前让我失望了。

Thank you for your help.谢谢您的帮助。

Your Foreign Key constraint is set to NOCHECK .您的外键约束设置为NOCHECK This is preventing the Foreign Key from being enforced which in turn is resulting in the DELETE CASCADE not occurring.这可以防止外键被强制执行,从而导致DELETE CASCADE不发生。

You can re-enable your constraint by running the following command:您可以通过运行以下命令重新启用约束:

ALTER TABLE [dbo].[Sprocs] CHECK CONSTRAINT [FK_Sprocs_Reports]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM