0
votes

I am using Entity Framework core and I added a model called CourseOffering to my project. This model is related to other classes like Section. I successfully created a migration for it. The problem is when I try to apply the migration to the database. I get the following error:

Introducing FOREIGN KEY constraint 'FK_CourseOfferings_Sections_SectionId' on table 'CourseOfferings' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.

I was trying to turn off cascade delete with FluentAPI, but I'm not entirely sure if that is the right solution to my problem. I mean is this totally safe to turn off cascade delete?

My CourseOffering model:

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; }
}

My Section Model:

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>();
    }
}

The migration created all the necessary foreign keys but there is cascade path that would cause cycles. I am not able to figure out what caused the cycle. Should I just turn off cascade delete with FluentAPI?

1

1 Answers

1
votes

I was trying to turn off cascade delete with FluentAPI, but I'm not entirely sure if that is the right solution to my problem. I mean is this totally safe to turn off cascade delete?

Yes! This is the appropriate solution in this case! Moreover yes! This is completely safe to turn off cascade delete using FluentAPI because FluentAPIwill generate constraint on database too.