EF 外键约束

EF Foreign Key constraint

我还在研究Entity Framework并尝试创建一个包含外键的模型。

但是当我尝试迁移代码时,出现了这个错误

Introducing FOREIGN KEY constraint 'FK_dbo.QuestionResults_dbo.QuestionsTables_QuetionsTableId' on table 'QuestionResults' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints

这些是我的模型类:

public class MainDetails
{
    [Key]
    public int Id { get; set; }
    public string Language { get; set; }

    [Required]
    public string CustomerName { get; set; }

    [Required]
    public string ContactNumber { get; set; }
    public string EmailAddress { get; set; }

    [DisplayName("Service Type")]
    [ForeignKey("QuestionsTable")]
    public int ServiceTypeId { get; set; }
    public virtual QuestionsTable QuestionsTable { get; set; }

    [Required]
    public string VehicleNumber { get; set; }

    [Required]
    public string ServiceLocation { get; set; }
    public string Suggestion { get; set; }
    public bool Status { get; set; } = true;

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    [Display(Name = "Created Date")]
    public DateTime CreatedDate { get; set; } = DateTime.Now;

    public virtual QuestionResults QuestionResults { get; set; }

    public virtual IList<QuestionResults> QuestionResultsMainlist { get; set; }
    public virtual IList<QuestionsTable> QuestionsTables { get; set; }
}

public class QuestionsTable
{
    [Key]
    public int Id { get; set; }
    public string ServiceType { get; set; }
    public string Question { get; set; }
    public virtual IList<MainDetails> MainDetailsServiceType { get; set; }
    public QuestionsTable()
    {
        MainDetailsServiceType = new List<MainDetails>();
    }
}

public class QuestionResults
{
    [Key]
    public int Id { get; set; }

    [DisplayName("MainDetail ID")]
    [ForeignKey("MainDetails")]
    public int MainDetailsId { get; set; }
    public virtual MainDetails MainDetails { get; set; }

    [DisplayName("MainDetail ID")]
    [ForeignKey("QuestionsTable")]
    public int QuetionsTableId { get; set; }
    public virtual QuestionsTable QuestionsTable { get; set; }

    [Required]
    public string CustoAnswer { get; set; }
}

这是我想要创建的 table 结构:

要解决此问题,您可以使用 EF 模型构建器

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{   
    modelBuilder.Entity<QuestionsTable>()
        .HasRequired(a => a.MainDetails)
        .WithOptionalDependent()
        .WillCascadeOnDelete(false); //This is the important row
}

您必须考虑您想要的关系类型以及是否明确指定外键。如果您之前没有见过这个模型构建器,请阅读此处:https://docs.microsoft.com/en-us/ef/core/modeling/


在分解你的场景后,我注意到了一些“奇怪的地方”。我将域模型中的噪音从你的例子减少到这个

public class MainDetails
{
    [Key]
    public int Id { get; set; }

    [ForeignKey("QuestionsTable")]
    public int ServiceTypeId { get; set; }
    
    public virtual QuestionsTable QuestionsTable { get; set; }

    public virtual QuestionResults QuestionResults { get; set; }

    public virtual IList<QuestionResults> QuestionResultsMainlist { get; set; }
    
    public virtual IList<QuestionsTable> QuestionsTables { get; set; }
}

public class QuestionsTable
{
    [Key]
    public int Id { get; set; }
    public string ServiceType { get; set; }
    public string Question { get; set; }
    public virtual IList<MainDetails> MainDetailsServiceType { get; set; }
}

public class QuestionResults
{
    [Key]
    public int Id { get; set; }

    [ForeignKey("MainDetails")]
    public int MainDetailsId { get; set; }
    public virtual MainDetails MainDetails { get; set; }

    [ForeignKey("QuestionsTable")]
    public int QuetionsTableId { get; set; }
    public virtual QuestionsTable QuestionsTable { get; set; }
}

我注意到了一些事情。

  • MainDetails 同时包含 One-To-Many (QuestionTable) 和 Many-To-Many (IList) 关系?我不确定你的意图
  • QuestionsResults 包含与未在 QuestionTable 中复制的两个实体的单一关系 class?如果是故意的就好了
  • ServiceType 是 QuestionsTable 中的字符串,但您希望 int 作为 MainDetails 中的外键?