错误:引入FOREIGN KEY约束可能会导致循环或多个级联路径

时间:2018-02-28 16:15:00

标签: c# asp.net-mvc entity-framework ef-code-first

主人的快速提问。

我有2个EF模型类:

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

基本上我想在代码优先创建一对多的关系。

但是当我尝试这样做时,我收到了这个错误:

  

介绍FOREIGN KEY约束&#39; FK_dbo.Teachers_dbo.Schools_SchoolId&#39;在桌子上&#39;教师&#39;可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

我在哪里弄错了?

修改

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

我没有收到此模型和密钥的任何错误..

1 个答案:

答案 0 :(得分:1)

删除从Note到School的关系,反之亦然。你可以通过与笔记有关系的老师来学校。

它会产生你的问题。