简体   繁体   English

EF-代码优先FOREIGN KEY约束可能会导致循环或多个级联路径

[英]EF - Code First FOREIGN KEY constraint may cause cycles or multiple cascade paths

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.' 我一直想先做一些实体框架代码,但是我一直坚持“ FOREIGN KEY约束可能会导致循环或多个级联路径”。 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. 我正在使用Web API,并且能够更改模型,因此请随时对其进行修改。 Could this be resolved using FluentAPI? 可以使用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. 在表'Students'上引入FOREIGN KEY约束'FK_dbo.Students_dbo.Courses_CourseId'可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.' 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

You have to use Fluent-API to disable delete / update. 您必须使用Fluent-API禁用删除/更新。 To do this, modify the OnModelCreating method: 为此,请修改OnModelCreating方法:

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. 我不确定是由课程类还是学生类引起的问题,如果这不起作用,请尝试在您的学生类上执行“ WillCascadeOnDelete(false)”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF FOREIGN KEY约束可能会导致循环或多个级联路径 - EF FOREIGN KEY constraint may cause cycles or multiple cascade paths ef核心2-在表&#39;Y&#39;上引入FOREIGN KEY约束&#39;X&#39;可能会导致循环或多个级联路径 - ef core 2 - Introducing FOREIGN KEY constraint 'X' on table 'Y' may cause cycles or multiple cascade paths FOREIGN KEY 约束可能会导致循环或多个级联路径。 ef core 3(根本无法创建数据库) - FOREIGN KEY constraint may cause cycles or multiple cascade paths. ef core 3 (cannot create a DB at all) 引入FOREIGN KEY约束可能会导致EF Core中的循环或多个级联路径 - Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths in EF Core 实体框架代码优先外键可能会导致循环或多个级联路径 - Entity Framework Code first Foreign Key may cause cycles or multiple cascade paths 在表上引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign key Constraint on table may cause cycles or multiple cascade paths SQL 错误:引入 FOREIGN KEY 约束可能会导致循环或多个级联路径。 实体框架核心 - SQL Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Entity Framework Core 在表“模型”上引入FOREIGN KEY约束“列”可能会导致循环或多个级联路径 - Introducing FOREIGN KEY constraint 'Column' on table 'Model' may cause cycles or multiple cascade paths 实体框架,外键约束可能会导致循环或多个级联路径 - Entity Framework, Foreign key constraint may cause cycles or multiple cascade paths 实体框架:在表 '' 上引入 FOREIGN KEY 约束 '' 可能会导致循环或多个级联路径 - Entity Framework: Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM