简体   繁体   English

避免“引入FOREIGN KEY约束可能会导致循环或多个级联路径”

[英]Avoiding 'Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths'

I reproduced a simple example of a problem I have with Entity Framework. 我转载了一个关于实体框架问题的简单示例。

I want to have three tables: 我想要三个表:

Users, Projects, WorkOrders

Table Users has information about users for all other tables (in example only two). Users具有所有其他表的Users信息(示例中只有两个)。 WorkOrders has information about which User has to work on this work order and to which Project it belongs. WorkOrders具有有关哪些User必须处理此工作订单以及该工作属于哪个Project

Here are the classes: 这些是类:

public class User
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<WorkOrder> WorkOrders { get; set; }
    public virtual ICollection<Project> Projects { get; set; }
}

public class Project
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int ManagerId { get; set; }
    public DateTime Start { get; set; }

    public virtual User Manager { get; set; }
    public virtual ICollection<WorkOrder> WorkOrders { get; set; }
}

public class WorkOrder
{
    [Key]
    public int Id { get; set; }
    public int Number { get; set; }
    public string Type { get; set; }
    public int AssigneeId { get; set; }
    public int ProjectId { get; set; }

    public virtual Project Project { get; set; }
    public virtual User Assignee { get; set; }
}

When I try to run the program, it throws an exception: 当我尝试运行程序时,它将引发异常:

'Introducing FOREIGN KEY constraint 'FK_dbo.WorkOrders_dbo.Projects_ProjectId' on table 'WorkOrders' may cause cycles or multiple cascade paths. 在表'WorkOrders'上引入FOREIGN KEY约束'FK_dbo.WorkOrders_dbo.Projects_ProjectId'可能会导致循环或多个级联路径。 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约束。

Then I went another way. 然后我走了另一条路。 I tried DB-first approach with EF. 我尝试使用EF进行数据库优先的方法。 I first created the tables and connections in SQL Server Management Studio: 我首先在SQL Server Management Studio中创建了表和连接:

图

Then the generated models by EF look almost the same as mine, with code-first approach. 然后,使用代码优先方法,EF生成的模型与我的模型几乎相同。

public partial class User
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public User()
    {
        this.Project = new HashSet<Project>();
        this.WorkOrder = new HashSet<WorkOrder>();
    }

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

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Project> Project { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<WorkOrder> WorkOrder { get; set; }
}

public partial class Project
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Project()
    {
        this.WorkOrder = new HashSet<WorkOrder>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public int ManagerId { get; set; }
    public System.DateTime Start { get; set; }

    public virtual User User { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<WorkOrder> WorkOrder { get; set; }
}

public partial class WorkOrder
{
    public int Id { get; set; }
    public int Number { get; set; }
    public string Type { get; set; }
    public int AssigneeId { get; set; }
    public int ProjectId { get; set; }

    public virtual Project Project { get; set; }
    public virtual User User { get; set; }
}

So the code is almost identical, except the SuppressMesages and constructors in WorkOrder and User classes. 因此,除了WorkOrderUser类中的SuppressMesages和构造函数外,代码几乎完全相同。 The second approach works. 第二种方法可行。

I would like to know, what makes the difference? 我想知道,有什么区别? Also the context class is identical as my own. 上下文类也和我自己的一样。 Where or how are defined this FK constraints or cascade delete settings? FK约束或级联删除设置在何处或如何定义?

It is unfortunate default behavior of EF code first to crate FK with on delete cascade. 不幸的是,EF代码的默认行为是首先在删除级联上创建FK。 So while defining relations you need to simply change this setting: 因此,在定义关系时,您只需更改此设置即可:

//in context
protected override void OnModelCreating(DbModelBuilder modelBuilder) { 
    modelBuilder.Entity<Project>()
        .HasRequired<User>(s => s.User)
        .WithMany()
        .WillCascadeOnDelete(false);

Also there might be a convention for it in modelBuilder.Conventions if you want to just change behavior for all FK. 如果只想更改所有FK的行为,则在modelBuilder.Conventions可能也有约定。

暂无
暂无

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

相关问题 当不需要属性时,引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign Key Constraint may cause cycles or multiple cascade paths when property is not required 错误:引入FOREIGN KEY约束可能会导致循环或多个级联路径-为什么? - Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why? 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 在表上引入外键约束可能会导致循环或多个级联路径 - 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 实体框架:在表 '' 上引入 FOREIGN KEY 约束 '' 可能会导致循环或多个级联路径 - Entity Framework: Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths 引入FOREIGN KEY约束可能会导致EF Core中的循环或多个级联路径 - Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths in EF Core 错误:引入FOREIGN KEY约束可能会导致循环或多个级联路径 - Error: introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths 表&#39;UsageSummaries&#39;上的多态与引入FOREIGN KEY约束可能会导致循环或多个级联路径 - Polymorphism vs Introducing FOREIGN KEY constraint '' on table 'UsageSummaries' may cause cycles or multiple cascade paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM