简体   繁体   English

外键约束可能导致循环或多个级联路径 ASP.NET Core MVC

[英]Foreign key constraint may cause cycles or multiple cascade paths ASP.NET Core MVC

I'm doing a project using ASP.NET Core MVC.我正在使用 ASP.NET Core MVC 做一个项目。 I created my models and when I update the migration I got this error.我创建了我的模型,当我更新迁移时,我得到了这个错误。

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

I went through the code and I hope there is nothing wrong with the foreign key assigned.我浏览了代码,希望分配的外键没有问题。

I hope the issue is Branch and Country both contain the CountryId for the foreign key.我希望问题是BranchCountry都包含外键的CountryId It should be connected to both tables connected with the Country table.它应该连接到与Country表相连的两个表。 But here it says can't create the table because of the cycle or multiple cascade paths.但是这里它说由于循环或多个级联路径而无法创建表。

So how to avoid this and do the migrations?那么如何避免这种情况并进行迁移呢? Or the way I connected 3 tables are wrong?还是我连接 3 个表的方式不对?

Could you help me with this?你能帮我解决这个问题吗?

public class Countries
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Country_Name { get; set; }
    public string Country_Code { get; set; }
    public string Note { get; set; } = "N/A";
    public bool Status { get; set; } = true;
    public DateTime CreatedDate { get; set; } = DateTime.Now;
    public int CreateBy { get; set; }
    public DateTime ModifiedDate { get; set; } = DateTime.Now;
    public int ModifiedBy { get; set; }

    public virtual IList<Province> Province { get; set; }
    public virtual IList<Branch> Branch { get; set; }

    public Countries()
    {
        Province = new List<Province>();
        Branch = new List<Branch>();
    }
}

public class Province
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Province_Name { get; set; }
    public string Province_Code { get; set; }
    [Required]
    [ForeignKey("Countries")]
    public int Country_Id { get; set; }
    public virtual Countries Countries { get; set; }
    public string Note { get; set; } = "N/A";
    public bool Status { get; set; } = true;
    public DateTime CreatedDate { get; set; } = DateTime.Now;
    public int CreateBy { get; set; }
    public DateTime ModifiedDate { get; set; } = DateTime.Now;
    public int ModifiedBy { get; set; }

    public virtual IList<Cities> Cities { get; set; }
    public virtual IList<Branch> Branch { get; set; }

    public Province()
    {
        Cities = new List<Cities>();
        Branch = new List<Branch>();
    }
}

public class Cities
{
    [Key]
    public int Id { get; set; }
    public string City_Name { get; set; }
    [ForeignKey("Province")]
    public int Province_Id { get; set; }
    public virtual Province Province { get; set; }
    public string Postal_Code { get; set; }
    public string Istat_Code { get; set; }
    public string Note { get; set; } = "N/A";
    public bool Status { get; set; } = true;
    public DateTime CreatedDate { get; set; } = DateTime.Now;
    public int CreateBy { get; set; }
    public DateTime ModifiedDate { get; set; } = DateTime.Now;
    public int ModifiedBy { get; set; }

    public virtual IList<Branch> Branch { get; set; }

    public Cities()
    {
        Branch = new List<Branch>();
    }
}

public class Branch
{
    [Key]
    public int Id { get; set; }
    public string BranchName { get; set; }
    public string Note { get; set; }
    public int City_Id { get; set; }
    public virtual Cities Cities { get; set; }
    public int Province_Id { get; set; }
    public virtual Province Province { get; set; }
    public int Country_Id { get; set; }
    public virtual Countries Countries { get; set; }
    public string LocationEmailAddress { get; set; }
    public string LocationContactNumber { get; set; }
    public bool Status { get; set; } = true;
    public DateTime CreatedDate { get; set; } = DateTime.Now;
    public int CreateBy { get; set; }
    public DateTime ModifiedDate { get; set; } = DateTime.Now;
    public int ModifiedBy { get; set; }

    public virtual IList<EmployeeLocations> EmployeeLocations { get; set; }

    public Branch()
    {
        EmployeeLocations = new List<EmployeeLocations>();
    }
}

在此处输入图像描述

It was caused by multiple cascade paths:它是由多个级联路径引起的:

Branch-Country-Province-City分公司-国家-省-市

Branch-Province-City分省市

Branch-City分城

You could check this case for more details: Foreign key constraint may cause cycles or multiple cascade paths?您可以查看此案例以获取更多详细信息: 外键约束可能导致循环或多个级联路径?

Solution is very simple.解决方案非常简单。 You should remove ContryId from Branch , because ContryId is already referenced in Province .您应该从Branch中删除ContryId ,因为ContryId已在Province中引用。

暂无
暂无

声明:本站的技术帖子网页,遵循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 SQL 错误:引入 FOREIGN KEY 约束可能会导致循环或多个级联路径。 实体框架核心 - SQL Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Entity Framework Core 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 在表上引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign key Constraint on table may cause cycles or multiple cascade paths 在表“模型”上引入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 当不需要属性时,引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign Key Constraint may cause cycles or multiple cascade paths when property is not required
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM