只有当我在删除时明确暴露行为时,才会在迁移过程中创建数据库RestrictOnDelete (Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict).否则抛出异常:
The database is created during migration only when I clearly expose the behavior when deleting Restrict OnDelete (Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict). Otherwise, it throws an exception:
"引入 FOREIGN KEY 约束表上的FK_RegulatorySchedules_Stations_DispatchStationId"RegulatorySchedules"可能会导致循环或多个级联路径.指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他外键约束."
"Introducing FOREIGN KEY constraint 'FK_RegulatorySchedules_Stations_DispatchStationId' on table 'RegulatorySchedules' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints."
我需要删除表的 Station Stations 以及暴露为 NULL 的表相关属性 RegulatorySchedules DispatchStation 和 DestinationStation.但是当你删除一个我不能放的 SetNull 时,限制选项有一个例外.告诉我怎么样?
I need the removal of the Station Stations of the table and the table-related properties RegulatorySchedules DispatchStation and DestinationStation exposed to NULL. But Restrict option there is an exception when you delete a SetNull I can not put. Tell me how to be?
推荐答案所描述的问题"与实体框架无关 - 这是 MS SQL Server 本身的限制.具有多个 FK 的表可能只有其中一个可以使用 级联 删除.
Described "problem" is not related to Entity Framework - this is restriction of MS SQL Server itself. Table with several FKs may have only one of them with cascade delete.
因此,一旦您需要两个 FK 都具有级联 - 您应该在您的代码中实现这样的清理".将一个(或两个)FK 设置为 DeleteBehavior.Restrict,并在删除 Station 之前在您的控制器/服务中手动查找并删除所有相关的 RegulatorySchedule
So, as soon as you need both FKs to have cascade - you should implement such "cleanup" in your code. Set one (or both) FKs to DeleteBehavior.Restrict, and in your controller/service prior to removing Station manually find and delete all related RegulatorySchedule
更多推荐
Entity Framework Core 级联删除一对多关系
发布评论