【发布时间】:2019-06-06 02:56:21
【问题描述】:
我正在使用 Entity Framework 核心,并在我的项目中添加了一个名为 CourseOffering 的模型。该模型与 Section 等其他类相关。我成功地为它创建了一个迁移。问题是当我尝试将迁移应用到数据库时。我收到以下错误:
在表“CourseOfferings”上引入 FOREIGN KEY 约束“FK_CourseOfferings_Sections_SectionId”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 无法创建约束或索引。查看以前的错误。
我试图用 FluentAPI 关闭级联删除,但我不完全确定这是否是解决我的问题的正确方法。我的意思是关闭级联删除完全安全吗?
我的 CourseOffering 模型:
public class CourseOffering
{
public int Id { get; set; }
public Section Section { get; set; }
public Instructor Instructor { get; set; }
public Course Course { get; set; }
public AcademicSemester AcademicSemester { get; set; }
public int SectionId { get; set; }
public int InstructorId { get; set; }
public int? CourseId { get; set; }
public int AcademicSemesterId { get; set; }
}
我的截面模型:
public class Section
{
public int Id { get; set; }
[Required]
[StringLength(10)]
public string Name { get; set; }
public int EntranceYear { get; set; }
public int StudentCount { get; set; }
public Department Department { get; set; }
public ProgramType Program { get; set; }
public AdmissionLevel AdmissionLevel { get; set; }
public ICollection<RoomSectionAssignment> RoomAssignments { get; set; }
public int DepartmentId { get; set; }
public int ProgramTypeId { get; set; }
public int AdmissionLevelId { get; set; }
public Section()
{
RoomAssignments = new Collection<RoomSectionAssignment>();
}
}
迁移创建了所有必要的外键,但存在会导致循环的级联路径。我无法弄清楚是什么导致了循环。我应该使用 FluentAPI 关闭级联删除吗?
【问题讨论】:
标签: asp.net-core entity-framework-core