我正在尝试实现 Entity Framework core 5 并且我也是新手。以下是我正在尝试实施的三个模型。
在发布新问题之前,我检查了以下答案,但无法理解卡片示例。也许我下面列出的问题会帮助今天像我这样的其他人更好地理解它。
我的模型如下:
[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 约束。无法创建约束或索引。请参阅以前的错误。
但是,如果删除了导航属性,它可以正常工作。您可能会问为什么我需要这些导航属性。这是为了在视图上显示相关信息。
对解释或评论该概念的任何帮助将不胜感激。
谢谢你。
以下是所示模型中的级联删除路径
- 诊所 -> 咨询
- 医生 -> 咨询
- 患者 -> 咨询
- 医生 -> 患者 -> 咨询
问题(多个级联路径)是最后两个。如您所见,删除 a 时Doctor
,Consultation
可以直接删除链接Patient
记录或查看链接记录。由于这种可能性,一些数据库(主要是 SqlServer)拒绝级联删除选项,并要求您至少为形成循环的一个关系关闭它,并手动或通过触发器处理删除。
所以通常这就是当这种循环存在时你应该做的。
但这里看起来模型有问题。要么Patient
不应该链接到单个Doctor
,而是通过链接表和删除Patient.Doctor
导航属性(因此关联的 FK 关系)链接到多个,从而自然地打破多个级联路径,即Doctor
删除只删除诊所和患者的链接,而不是诊所和患者的链接他们自己。
或者,如果你想保持Patient
单身Doctor
关系,那么Consultation.Doctor
(以及相关的Consultation.DoctorId
FK 和关系)是多余的 - 咨询的医生可以通过 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] 删除。
我来说两句