Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF-代码第一外键约束可能导致循环或多个级联路径_C#_Sql_Entity Framework_Code First_Asp.net Web Api - Fatal编程技术网

C# EF-代码第一外键约束可能导致循环或多个级联路径

C# EF-代码第一外键约束可能导致循环或多个级联路径,c#,sql,entity-framework,code-first,asp.net-web-api,C#,Sql,Entity Framework,Code First,Asp.net Web Api,我已经开始尝试先编写一些实体框架代码,但我遇到了“外键约束可能会导致循环或多个级联路径”的问题 以下是我的课程: public class Course { public Course() { this.Subject = new HashSet<Subject>(); this.Student = new HashSet<Student>(); } public int Id { get; set; }

我已经开始尝试先编写一些实体框架代码,但我遇到了“外键约束可能会导致循环或多个级联路径”的问题

以下是我的课程:

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; }
}
我认为这是一个引用循环错误,但我不知道如何解决它。我正在使用WebAPI,我可以更改模型,所以请随意修改。这能用FluentAPI解决吗

这是个例外。它是在我第一次执行应用程序时抛出的:


在表“Students”上引入外键约束“FK_dbo.Students_dbo.Courses_CourseId”可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他外键约束。“

您必须使用Fluent API禁用删除/更新。为此,请修改OnModelCreating方法:

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

我不确定是课程类还是学生类引发了问题,如果这不起作用,请尝试在学生类上执行WillCascadeOnDeletefalse。

能否发布引发异常的确切文本?您应该能够找到代码首先不喜欢的类/属性的名称。什么时候抛出它:当您运行更新数据库时,当您第一次访问数据库时,还有其他什么吗?我已经将它添加到问题中。