引入FOREIGN KEY约束可能会导致循环或多个级联路径,同一模型

时间:2019-07-17 11:46:46

标签: c# ef-core-2.2

型号:

 public class Candidate
  {
    public int PCountryId { get; set; }
    public Country PCountry { get; set; }

    public int NCountryId { get; set; }
    public Country NCountry { get; set; }
 }

错误:

Introducing FOREIGN KEY constraint 'FK_tblDetails_tblCountries_NCountryId' on table 'tblDetails' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint

尝试过

 public int? NCountryId { get; set; }

 [ForeignKey("NCountryId")]
 public virtual Country NCountry { get; set; }

 foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
     {
         relationship.DeleteBehavior = DeleteBehavior.Restrict;
     }

0 个答案:

没有答案