【发布时间】:2021-10-06 13:52:20
【问题描述】:
我正在尝试实现实体框架核心 5 我也是新手。以下是我正在尝试实现的三个模型。
在发布新问题之前,我检查了以下答案,但无法理解卡片示例。也许下面列出的我的问题会帮助今天像我这样的其他人更好地理解它。
- Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?
- Foreign key constraint may cause cycles or multiple cascade paths?
我的模型如下:
[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 约束。 无法创建约束或索引。查看以前的错误。
但是,如果导航属性被删除,它可以正常工作。你可能会问我为什么需要这些导航属性。这是为了在视图上显示相关信息。
非常感谢您对解释或评论该概念的任何帮助。
谢谢。
【问题讨论】:
-
如果你也显示 Patient 类会很好
-
所示模型中没有可见的循环。请 (1) 提供第三个相关实体 (
Patient) (2) 您是否使用 EF Core 5,因此在Clinic和Doctor之间存在隐式多对多链接表? -
@Serge 我已将其添加到代码中。
-
@IvanStoev 1:添加到代码中。 2:是的
标签: asp.net-mvc asp.net-core entity-framework-core