struct&&assiQues
This commit is contained in:
@@ -14,7 +14,8 @@ namespace TechHelper.Context.Configuration
|
||||
// 2. 设置主键
|
||||
builder.HasKey(q => q.Id);
|
||||
|
||||
builder.HasIndex(q => q.QuestionText);
|
||||
builder.HasIndex(q => q.Title)
|
||||
.HasPrefixLength(20);
|
||||
|
||||
// 3. 配置列名、必需性、长度及其他属性
|
||||
|
||||
@@ -23,44 +24,37 @@ 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)
|
||||
builder.Property(q => q.Title)
|
||||
.HasColumnName("question_text")
|
||||
.IsRequired()
|
||||
.HasMaxLength(65535); // 对应 MaxLength(65535)
|
||||
|
||||
// QuestionType (枚举作为字符串存储)
|
||||
builder.Property(q => q.QuestionType)
|
||||
builder.Property(q => q.Type)
|
||||
.HasColumnName("question_type")
|
||||
.IsRequired()
|
||||
.HasConversion<string>() // <-- 重要:将枚举存储为字符串
|
||||
.HasMaxLength(20); // <-- 应用最大长度
|
||||
.HasMaxLength(20);
|
||||
|
||||
// CorrectAnswer
|
||||
builder.Property(q => q.CorrectAnswer)
|
||||
builder.Property(q => q.Answer)
|
||||
.HasColumnName("correct_answer")
|
||||
.HasMaxLength(65535); // 对应 MaxLength(65535)
|
||||
.HasMaxLength(65535);
|
||||
|
||||
// DifficultyLevel (枚举作为字符串存储)
|
||||
builder.Property(q => q.DifficultyLevel)
|
||||
.HasColumnName("difficulty_level")
|
||||
.HasConversion<string>() // <-- 重要:将枚举存储为字符串
|
||||
.HasMaxLength(10); // <-- 应用最大长度
|
||||
// DifficultyLevel 属性没有 [Required],所以默认是可选的
|
||||
.HasMaxLength(10);
|
||||
|
||||
|
||||
// SubjectArea
|
||||
builder.Property(q => q.SubjectArea)
|
||||
.HasColumnName("subject_area")
|
||||
.HasConversion<string>() // <--- 重要:将枚举存储为字符串
|
||||
.HasMaxLength(100); // <--- 应用最大长度 (从原 StringLength 继承)
|
||||
.HasMaxLength(100);
|
||||
|
||||
// CreatedBy
|
||||
builder.Property(q => q.CreatedBy)
|
||||
builder.Property(q => q.CreatorId)
|
||||
.HasColumnName("created_by")
|
||||
.IsRequired();
|
||||
|
||||
@@ -91,7 +85,7 @@ namespace TechHelper.Context.Configuration
|
||||
// 假设 `User` 实体中有一个名为 `CreatedQuestions` 的 `ICollection<Question>` 集合属性。
|
||||
builder.HasOne(q => q.Creator) // 当前 Question 有一个 Creator
|
||||
.WithMany(u => u.CreatedQuestions) // 那个 Creator 可以创建多个 Question
|
||||
.HasForeignKey(q => q.CreatedBy) // 外键是 Question.CreatedBy
|
||||
.HasForeignKey(q => q.CreatorId) // 外键是 Question.CreatedBy
|
||||
.OnDelete(DeleteBehavior.Restrict); // 当 User (Creator) 被删除时,如果还有他/她创建的 Question,则会阻止删除。
|
||||
// 由于 CreatedBy 是 [Required],Prevent/Restrict 是一个安全的选择。
|
||||
|
||||
@@ -105,11 +99,24 @@ namespace TechHelper.Context.Configuration
|
||||
.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
|
||||
|
||||
builder.HasOne(q => q.KeyPoint)
|
||||
.WithMany(kp => kp.Questions)
|
||||
.HasForeignKey(q => q.KeyPointId)
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
|
||||
builder.HasOne(q => q.Lesson)
|
||||
.WithMany(kp => kp.Questions)
|
||||
.IsRequired(false)
|
||||
.HasForeignKey(q => q.LessonId)
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
|
||||
builder.HasOne(q => q.ParentQuestion)
|
||||
.WithMany(pq => pq.ChildrenQuestion)
|
||||
.IsRequired(false)
|
||||
.HasForeignKey(q => q.ParentQuestionId)
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user