1
votes

I have been up to trying to do some Entity Framework Code First, but I am stuck with the 'FOREIGN KEY constraint may cause cycles or multiple cascade paths.' problem.

Here are my classes:

public class Course
{
    public Course()
    {
        this.Subject = new HashSet<Subject>();
        this.Student = new HashSet<Student>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Shift { get; set; }
    public int Room { get; set; }

    public virtual ICollection<Subject> Subject { get; set; }
    public virtual ICollection<Student> Student { get; set; }
}

public class Subject
{
    public Subject()
    {
        this.Deliverable = new HashSet<Deliverable>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public int StartsAt { get; set; }
    public int FinishesAt { get; set; }
    public System.TimeSpan Weekdays { get; set; }
    public int CourseId { get; set; }
    public int TeacherId { get; set; }

    public virtual Course Course { get; set; }
    public virtual Teacher Teacher { get; set; }
    public virtual ICollection<Deliverable> Deliverable { get; set; }
}

public class Student : Person
{
    public Student()
    {
        this.Deliverable = new HashSet<Deliverable>();
    }

    public decimal Average { get; set; }
    public int CourseId { get; set; }

    public virtual Course Course { get; set; }
    public virtual ICollection<Deliverable> Deliverable { get; set; }
}

public class Teacher : Person
{
    public Teacher()
    {
        this.Subject = new HashSet<Subject>();
    }


    public virtual ICollection<Subject> Subject { get; set; }
}

public class Deliverable
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Mark { get; set; }
    public System.DateTime DeliveredDate { get; set; }
    public bool Delivered { get; set; }
    public System.DateTime AnnounceDate { get; set; }
    public int SubjectId { get; set; }
    public int StudentId { get; set; }

    public virtual Subject Subject { get; set; }
    public virtual Student Student { get; set; }
}

I think it's a reference looping error, but I can't realize the approach on how to resolve it. I'm using Web API and I'm able to change the model, so feel free to modify it please. Could this be resolved using FluentAPI?

Here is the exception. It is thrown the first time I have executed the application:

'Introducing FOREIGN KEY constraint 'FK_dbo.Students_dbo.Courses_CourseId' on table 'Students' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.'

1
Can you post the exact text of the exception being thrown? You should be able to find the name of the classes/properties that Code First doesn't like. And when is it thrown: when you run update-database, when you access the database for the first time, something else? - Troy Carlson
I have added it to the question. - Sweeden

1 Answers

0
votes

You have to use Fluent-API to disable delete / update. To do this, modify the OnModelCreating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<Course>().HasMany(c => c.Student).WillCascadeOnDelete(false);
}

I'm not sure if it's the Course class or the Student class who causes issues, if this is not working, try to do a "WillCascadeOnDelete(false)" on your Student class.