重构试卷相关内容
This commit is contained in:
@@ -14,6 +14,8 @@ namespace TechHelper.Context.Configuration
|
||||
// 2. 设置主键
|
||||
builder.HasKey(q => q.Id);
|
||||
|
||||
builder.HasIndex(q => q.QuestionText);
|
||||
|
||||
// 3. 配置列名、必需性、长度及其他属性
|
||||
|
||||
// Id
|
||||
@@ -21,6 +23,11 @@ namespace TechHelper.Context.Configuration
|
||||
.HasColumnName("id");
|
||||
// 对于 Guid 类型的主键,EF Core 默认由应用程序生成值,无需 ValueGeneratedOnAdd()
|
||||
|
||||
builder.Property(q => q.QuestionGroupId)
|
||||
.HasColumnName("question_group_id")
|
||||
.IsRequired(false); // 可为空,因为题目不一定属于某个题组
|
||||
|
||||
|
||||
// QuestionText
|
||||
builder.Property(q => q.QuestionText)
|
||||
.HasColumnName("question_text")
|
||||
@@ -97,6 +104,13 @@ namespace TechHelper.Context.Configuration
|
||||
builder.HasMany(q => q.AssignmentQuestions) // 当前 Question 有多个 AssignmentQuestion
|
||||
.WithOne(aq => aq.Question); // 每一个 AssignmentQuestion 都有一个 Question
|
||||
// .HasForeignKey(aq => aq.QuestionId); // 外键的配置应在 `AssignmentQuestionConfiguration` 中进行
|
||||
|
||||
builder.HasOne(q => q.QuestionGroup) // Question 实体中的 QuestionGroup 导航属性
|
||||
.WithMany(qg => qg.Questions) // QuestionGroup 实体中的 Questions 集合
|
||||
.HasForeignKey(q => q.QuestionGroupId) // Question 实体中的 QuestionGroupId 外键
|
||||
.IsRequired(false) // QuestionGroupId 在 Question 实体中是可空的
|
||||
.OnDelete(DeleteBehavior.SetNull); // 如果 QuestionGroup 被删除,关联的 Question 的外键设置为 NULL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user