Entity Framework Core 5 - 在表上引入 FOREIGN KEY 约束可能会导致循环或多个级联路径

法赫姆·拉希德

我正在尝试实现 Entity Framework core 5 并且我也是新手。以下是我正在尝试实施的三个模型。

在发布新问题之前,我检查了以下答案,但无法理解卡片示例。也许我下面列出的问题会帮助今天像我这样的其他人更好地理解它。

  1. 引入 FOREIGN KEY 约束可能会导致循环或多个级联路径 - 为什么?
  2. 外键约束可能导致循环或多个级联路径?

我的模型如下:

[Table("Clinics")]
public class Clinic
{
    public int ClinicID { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public string Address { get; set; }

    public List<Doctor> DoctorsAvailable { get; set; } = new List<Doctor>();
}


[Table("Doctors")]
public class Doctor
{
    public int ID { get; set; }

    public string Name { get; set; }

    public DoctorsSpecilization Specilization { get; set; }

    public string PhoneNumber { get; set; }

    public string Email { get; set; }

    public List<Clinic> ClinicsAvailableAt { get; set; } = new List<Clinic>();
}

 [Table("Patients")]
public class Patient
{
    public int PatientID { get; set; }

    public string Name { get; set; }

    public string Address { get; set; }

    public string PhoneNumber { get; set; }

    public string Email { get; set; }

    public int DoctorID { get; set; }

    public Doctor Doctor { get; set; }
}


[Table("Consultations")]
public class Consultation
{
    public int ID { get; set; }

    public int ClinicID { get; set; }

    public int DoctorID { get; set; }

    public int PatientID { get; set; }

    public Clinic Clinic { get; set; }

    [ForeignKey("DoctorID")]
    public Doctor Doctor { get; set; }

    [ForeignKey("PatientID")]
    public Patient Patient { get; set; }

    public DateTime StartTime { get; set; }

    public DateTime EndTime { get; set; }

}

问题是咨询模型中医生和患者的导航属性。当我尝试“更新数据库”时,它失败了

在表 'Consultations' 上引入 FOREIGN KEY 约束 'FK_Consultations_Patients_PatientID' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。无法创建约束或索引。请参阅以前的错误。

但是,如果删除了导航属性,它可以正常工作。您可能会问为什么我需要这些导航属性。这是为了在视图上显示相关信息。

对解释或评论该概念的任何帮助将不胜感激。

谢谢你。

伊万·斯托耶夫

以下是所示模型中的级联删除路径

  1. 诊所 -> 咨询
  2. 医生 -> 咨询
  3. 患者 -> 咨询
  4. 医生 -> 患者 -> 咨询

问题(多个级联路径)是最后两个。如您所见,删除 a 时DoctorConsultation可以直接删除链接Patient记录或查看链接记录。由于这种可能性,一些数据库(主要是 SqlServer)拒绝级联删除选项,并要求您至少为形成循环的一个关系关闭它,并手动或通过触发器处理删除。

所以通常这就是当这种循环存在时你应该做的。

但这里看起来模型有问题。要么Patient不应该链接到单个Doctor,而是通过链接表和删除Patient.Doctor导航属性(因此关联的 FK 关系)链接多个,从而自然地打破多个级联路径,即Doctor删除只删除诊所和患者的链接,而不是诊所和患者的链接他们自己。

或者,如果你想保持Patient单身Doctor关系,那么Consultation.Doctor(以及相关的Consultation.DoctorIdFK 和关系)是多余的 - 咨询的医生可以通过 Consulting.Patient.Doctor 获得)。所以删除它,这也将解决多个级联路径问题,因为将不再有Doctor -> Consultation级联删除链接。

为清楚起见,第一个建议选项需要以下模型更改:


[Table("Clinics")]
public class Clinic // Unchanged
{
    public int ClinicID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Address { get; set; }
    public List<Doctor> DoctorsAvailable { get; set; } = new List<Doctor>();
}

[Table("Doctors")]
public class Doctor
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }
    public List<Clinic> ClinicsAvailableAt { get; set; } = new List<Clinic>();
    public ICollection<Patient> Patients { get; set; } // <-- added
}

[Table("Patients")]
public class Patient
{
    public int PatientID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }
    //public int DoctorID { get; set; } <-- removed
    //public Doctor Doctor { get; set; } <-- removed
    public ICollection<Doctor> Doctors { get; set; } // <-- added
}


[Table("Consultations")]
public class Consultation // Unchanged
{
    public int ID { get; set; }
    public int ClinicID { get; set; }
    public int DoctorID { get; set; }
    public int PatientID { get; set; }
    public Clinic Clinic { get; set; }
    [ForeignKey("DoctorID")]
    public Doctor Doctor { get; set; }
    [ForeignKey("PatientID")]
    public Patient Patient { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

和选项2:


[Table("Clinics")]
public class Clinic // Unchanged
{
    public int ClinicID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Address { get; set; }
    public List<Doctor> DoctorsAvailable { get; set; } = new List<Doctor>();
}

[Table("Doctors")]
public class Doctor // Unchanged
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }
    public List<Clinic> ClinicsAvailableAt { get; set; } = new List<Clinic>();
}

[Table("Patients")]
public class Patient // Unchanged
{
    public int PatientID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }
    public int DoctorID { get; set; }
    public Doctor Doctor { get; set; }
}


[Table("Consultations")]
public class Consultation
{
    public int ID { get; set; }
    public int ClinicID { get; set; }
    //public int DoctorID { get; set; } <-- removed
    public int PatientID { get; set; }
    public Clinic Clinic { get; set; }
    //[ForeignKey("DoctorID")]
    //public Doctor Doctor { get; set; } <-- removed
    [ForeignKey("PatientID")]
    public Patient Patient { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ef core 2 - 在表“Y”上引入 FOREIGN KEY 约束“X”可能会导致循环或多个级联路径

在表 'Y' 上引入 FOREIGN KEY 约束 'X' 可能会导致循环或多个级联路径

在表“ Employer”上引入FOREIGN KEY约束“ FK__Employer__postal__59FA5E80”可能会导致循环或多个级联路径

如何防止错误:在表上引入FOREIGN KEY约束可能会导致循环或多个级联路径

在表 'ReservedSeats' 上引入 FOREIGN KEY 约束 'FK_ReservedSeats_Seats_SeatId' 可能会导致循环或多个级联路径

引入FOREIGN KEY约束可能会导致循环或多个级联路径

引入FOREIGN KEY约束可能会导致循环或多个级联路径-为什么?

SQL Server 引入 FOREIGN KEY 约束可能会导致循环或多个级联路径

在表上引入FOREIGN KEY约束可能会导致循环或多个级联路径,即使在完全删除受影响的字段之后也是如此

引入FOREIGN KEY约束可能会导致循环或多个级联路径。指定删除时不执行任何操作

SQL错误:引入FOREIGN KEY约束可能会导致循环或多个级联路径。实体框架核心

可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束

INSERT语句与Entity Framework Core中的FOREIGN KEY约束冲突

FOREIGN KEY可能会导致循环或多个级联路径异常

INSERT 语句与 FOREIGN KEY SAME TABLE 约束冲突 | ASP.NET Core & Entity Framework Core Code First

由于级联删除而导致的“ FOREIGN KEY约束引入”

实体框架核心:如何解决?引入外键约束可能会导致循环或多个级联路径

违反Entity Framework Core中的PRIMARY KEY约束

使用Entity Framework Core获取多个表

具有相同必需属性类型的EF Core TPH实体导致“引入FOREIGN KEY约束”异常

EF Core DeleteBehavior.Cascade可能会导致循环或多个级联路径

如何在Entity Framework Core中的多个表上通过联接调用存储过程?

在Entity Framework Core上添加迁移时出现NET 5错误

使用Entity Framework Core共享表

NET 4.6.1上的Entity Framework Core 2.0

为什么运行时表达式会导致 Entity Framework Core 5 的缓存发生冲突?

在 Entity Framework Core 中使用 group by 连接多个表会引发异常

Entity Framework Core:获取行的循环参考

无法执行DbCommand引入的FOREIGN KEY约束