I'm doing a project using 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. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints
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. It should be connected to both tables connected with the Country
table. 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?
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>();
}
}
CodePudding user response:
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?
CodePudding user response:
Solution is very simple. You should remove ContryId
from Branch
, because ContryId
is already referenced in Province
.