Entity Framework Core 级联删除一对多关系

编程入门 行业动态 更新时间:2024-10-28 21:20:23
本文介绍了Entity Framework Core 级联删除一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public class Station : IEntitie { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual ICollection<RegulatorySchedule> RegulatoryScheduleDispatchStations { get; set; } public virtual ICollection<RegulatorySchedule> RegulatoryScheduleDestinationStations { get; set; } } public class RegulatorySchedule : IEntitie { [Key] public int Id { get; set; } public virtual Station DispatchStation { get; set; } public virtual Station DestinationStation { get; set; } } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<RegulatorySchedule>() .HasOne(s => s.DestinationStation) .WithMany(s => s.RegulatoryScheduleDestinationStations) .OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict); modelBuilder.Entity<RegulatorySchedule>() .HasOne(s => s.DispatchStation) .WithMany(s => s.RegulatoryScheduleDispatchStations) .OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict); }

只有当我在删除时明确暴露行为时,才会在迁移过程中创建数据库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 级联删除一对多关系

本文发布于:2023-11-15 12:16:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1594354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:级联   关系   Entity   Framework   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!