Ersin Devrim
Ersin Devrim

Reputation: 117

Error: introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths

A quick question for masters.

I got 2 EF model classes:

public class School
{
        public int Id { get; set; }
        [DisplayName("School")]
        public string Name { get; set; }

        public List<Teacher> Teachers { get; set; }
        public List<Note> Notes { get; set; }
}

public class Teacher
{
    public int Id { get; set; }
    [DisplayName("Öğretmen")]
    public string Name { get; set; }
    public int SchoolId { get; set; }

    public School School { get; set; }
    public List<Note> Notes { get; set; }
}

Basically I want to create an one to many relationship in code-first.

But when I try to do that, I get this error:

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

Where have I made a mistake?

EDIT

  public class Note
    {
        public int Id { get; set; }
        [Required,DisplayName("Başlık"), StringLength(50)]
        public string Title { get; set; }
        [Required,DisplayName("Açıklama"), StringLength(4000)]
        public string Description { get; set; }
        public string File { get; set; }
        public DateTime UploadDate { get; set; }
        public bool IsApproved { get; set; }
        public int SchoolId { get; set; }
        public int OwnerId { get; set; }
        public int TeacherId { get; set; }


        //Keys

        public School School { get; set; }
        public Teacher Teacher { get; set; }
        public List<Comment> Comments { get; set; }
    }

I didnt get any error for this Model and Keys..

Upvotes: 0

Views: 360

Answers (1)

ChW
ChW

Reputation: 3348

Remove the relationship from Note to School and vice versa. You could get the school by the teacher that has a relationship to the note.

It will produce your issue.

Upvotes: 1

Related Questions