我正在研究Entity Framework核心,需要实现同一个问题'问题'实体进入QuestionHierarchy实体。在数据迁移期间,我收到以下错误。我还有下面的配置类
Introducing FOREIGN KEY constraint 'FK_QuestionHierarchy_Questions_ParentQuestionId' on table 'QuestionHierarchy' 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 QuestionDataModel : IDataModel<Guid>
{
public QuestionDataModel()
{
ParentQuestionsInHierarchy = new HashSet<QuestionHierarchyDataModel>();
ChildQuestionsInHierarchy = new HashSet<QuestionHierarchyDataModel>();
}
public Guid Id { get; set; }
public virtual ICollection<QuestionHierarchyDataModel> ParentQuestionsInHierarchy { get; set; }
public virtual ICollection<QuestionHierarchyDataModel> ChildQuestionsInHierarchy { get; set; }
}
public class QuestionHierarchyDataModel
{
public QuestionHierarchyDataModel()
{
}
public Guid Id { get; set; }
public Guid ParentQuestionId { get; set; }
public virtual QuestionDataModel ParentQuestions{ get; set; }
public Guid ChildQuestionId { get; set; }
public virtual QuestionDataModel ChildQuestions { get; set; }
}
public void Configure(EntityTypeBuilder<QuestionDataModel> builder)
{
builder.ToTable("Questions");
builder.HasKey(question => question.Id);
builder.HasMany(question => question.ParentQuestionsInHierarchy);
builder.HasMany(question => question.ChildQuestionsInHierarchy);
}
public void Configure(EntityTypeBuilder<QuestionHierarchyDataModel> builder)
{
builder.ToTable("QuestionHierarchy");
builder.HasKey(questionHierarchy => questionHierarchy.Id);
builder
.HasOne(questionHierarchy => questionHierarchy.ParentQuestions)
.WithMany(question => question.ParentQuestionsInHierarchy)
.HasForeignKey(questionHierarchy => questionHierarchy.ParentQuestionId);
builder
.HasOne(questionHierarchy => questionHierarchy.ChildQuestions)
.WithMany(question => question.ChildQuestionsInHierarchy)
.HasForeignKey(questionHierarchy => questionHierarchy.ChildQuestionId);
}