AsiignmentStruct
This commit is contained in:
@@ -13,7 +13,6 @@ namespace TechHelper.Context
|
||||
|
||||
public DbSet<AssignmentClass> AssignmentClasses { get; set; }
|
||||
public DbSet<Assignment> Assignments { get; set; }
|
||||
public DbSet<AssignmentQuestion> AssignmentGroups { get; set; }
|
||||
public DbSet<AssignmentQuestion> AssignmentQuestions { get; set; }
|
||||
public DbSet<Class> Classes { get; set; }
|
||||
public DbSet<ClassTeacher> ClassStudents { get; set; }
|
||||
@@ -21,6 +20,7 @@ namespace TechHelper.Context
|
||||
public DbSet<Question> Questions { get; set; }
|
||||
public DbSet<Submission> Submissions { get; set; }
|
||||
public DbSet<SubmissionDetail> SubmissionDetails { get; set; }
|
||||
public DbSet<QuestionContext> QuestionContexts { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
@@ -28,7 +28,6 @@ namespace TechHelper.Context
|
||||
builder.ApplyConfiguration(new RoleConfiguration());
|
||||
builder.ApplyConfiguration(new AssignmentConfiguration());
|
||||
builder.ApplyConfiguration(new AssignmentClassConfiguration());
|
||||
builder.ApplyConfiguration(new AssignmentGroupConfiguration());
|
||||
builder.ApplyConfiguration(new AssignmentQuestionConfiguration());
|
||||
builder.ApplyConfiguration(new ClassConfiguration());
|
||||
builder.ApplyConfiguration(new ClassStudentConfiguration());
|
||||
|
@@ -50,10 +50,11 @@ namespace TechHelper.Context
|
||||
// =============================================================
|
||||
// ENTITY -> DTO Mappings (用于读取/查询)
|
||||
// =============================================================
|
||||
CreateMap<Assignment, AssignmentDto>()
|
||||
.ForMember(dest => dest.ExamStruct, opt => opt.MapFrom(src => src.ExamStruct));
|
||||
CreateMap<Assignment, AssignmentDto>();
|
||||
|
||||
CreateMap<QuestionContext, QuestionContextDto>().ReverseMap();
|
||||
|
||||
|
||||
CreateMap<AssignmentStruct, AssignmentStructDto>(); // 直接映射,因为成员现在对等了
|
||||
|
||||
// 新增!从实体到新的 DTO 的映射
|
||||
CreateMap<AssignmentQuestion, AssignmentQuestionDto>();
|
||||
@@ -64,7 +65,6 @@ namespace TechHelper.Context
|
||||
// DTO -> ENTITY Mappings (用于创建/更新) - 现在变得极其简单!
|
||||
// =================================================================
|
||||
CreateMap<AssignmentDto, Assignment>();
|
||||
CreateMap<AssignmentStructDto, AssignmentStruct>();
|
||||
|
||||
// 新增!从新的 DTO 到实体的映射
|
||||
CreateMap<AssignmentQuestionDto, AssignmentQuestion>();
|
||||
|
@@ -1,83 +0,0 @@
|
||||
using Entities.Contracts;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace TechHelper.Context.Configuration
|
||||
{
|
||||
public class AssignmentGroupConfiguration : IEntityTypeConfiguration<AssignmentStruct>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<AssignmentStruct> builder)
|
||||
{
|
||||
// 1. 设置表名
|
||||
// 将此实体映射到数据库中名为 "assignment_detail" 的表。
|
||||
builder.ToTable("assignment_group");
|
||||
|
||||
// 2. 配置主键
|
||||
// Id 属性作为主键。
|
||||
builder.HasKey(ag => ag.Id);
|
||||
|
||||
// 3. 配置列名、必需性、长度和默认值
|
||||
|
||||
// 配置 Id 属性对应的数据库列名为 "id"。
|
||||
builder.Property(ag => ag.Id)
|
||||
.HasColumnName("id");
|
||||
// EF Core 默认 Guid 类型主键由应用程序生成,因此无需 ValueGeneratedOnAdd()。
|
||||
|
||||
// 配置 AssignmentId 属性对应的数据库列名为 "assignment",并设置为必需字段。
|
||||
builder.Property(ag => ag.AssignmentId)
|
||||
.HasColumnName("assignment");
|
||||
|
||||
// 配置 Title 属性对应的数据库列名为 "title",设置为必需字段,并设置最大长度。
|
||||
builder.Property(ag => ag.Title)
|
||||
.HasColumnName("title")
|
||||
.IsRequired()
|
||||
.HasMaxLength(65535); // 对应 MaxLength(65535)
|
||||
|
||||
// 配置 Descript 属性对应的数据库列名为 "descript",并设置最大长度。
|
||||
builder.Property(ag => ag.Description)
|
||||
.HasColumnName("descript")
|
||||
.HasMaxLength(65535); // 对应 MaxLength(65535)
|
||||
|
||||
// 配置 TotalPoints 属性对应的数据库列名为 "total_points"。
|
||||
// TotalPoints 是 decimal? 类型,默认就是可选的,无需 IsRequired(false)。
|
||||
builder.Property(ag => ag.Score)
|
||||
.HasColumnName("total_points");
|
||||
|
||||
// 配置 Number 属性对应的数据库列名为 "number"。
|
||||
builder.Property(ag => ag.Index)
|
||||
.HasColumnName("number")
|
||||
.IsRequired(); // byte 默认非空,显式 IsRequired 增加可读性。
|
||||
|
||||
// 配置 ParentGroup 属性对应的数据库列名为 "sub_group"。
|
||||
// ParentGroup 是 Guid? 类型,默认就是可选的,无需 IsRequired(false)。
|
||||
builder.Property(ag => ag.ParentStructId)
|
||||
.HasColumnName("parent_group")
|
||||
.IsRequired(false);
|
||||
|
||||
// 配置 IsDeleted 属性对应的数据库列名为 "deleted",并设置默认值为 false。
|
||||
builder.Property(ag => ag.IsDeleted)
|
||||
.HasColumnName("deleted")
|
||||
.HasDefaultValue(false); // 适用于软删除策略
|
||||
|
||||
// 4. 配置导航属性和外键关系
|
||||
|
||||
// 配置 AssignmentGroup 到 Assignment 的多对一关系。
|
||||
// 一个 AssignmentGroup 记录属于一个 Assignment。
|
||||
builder.HasOne(ag => ag.Assignment) // 当前 AssignmentGroup 有一个 Assignment
|
||||
.WithOne(a => a.ExamStruct) // 该 Assignment 可以有多个 AssignmentGroup 记录
|
||||
.HasForeignKey<AssignmentStruct>(ag => ag.AssignmentId); // 通过 AssignmentId 建立外键
|
||||
|
||||
|
||||
// 配置 AssignmentGroup 到 AssignmentGroup 的自引用关系(父子关系)。
|
||||
// 一个 AssignmentGroup 可以有一个父 AssignmentGroup (SubAssignmentGroup)。
|
||||
// 假设父 AssignmentGroup 实体中有一个名为 ChildAssignmentGroups 的集合属性来表示它所包含的所有子组。
|
||||
builder.HasOne(ag => ag.ParentStruct) // 当前 AssignmentGroup 有一个父 AssignmentGroup
|
||||
.WithMany(parentAg => parentAg.ChildrenGroups) // 该父 AssignmentGroup 可以有多个子 AssignmentGroup
|
||||
.HasForeignKey(ag => ag.ParentStructId) // 通过 SubGroup 建立外键
|
||||
.IsRequired(false) // SubGroup 是可空的 (Guid?),所以这个关系是可选的。
|
||||
.OnDelete(DeleteBehavior.SetNull); // 当父 AssignmentGroup 被删除时,其子 AssignmentGroup 的 SubGroup 外键将被设置为 NULL。
|
||||
// 如果你希望父组被删除时子组不能脱离父组(即不允许父组被删除),
|
||||
// 可以使用 DeleteBehavior.Restrict 或 DeleteBehavior.NoAction。
|
||||
}
|
||||
}
|
||||
}
|
@@ -38,12 +38,6 @@ namespace TechHelper.Context.Configuration
|
||||
builder.Property(aq => aq.Score)
|
||||
.HasColumnName("score");
|
||||
|
||||
// 配置 AssignmentGroupId 列
|
||||
// 该列在数据库中名为 "detail_id"
|
||||
builder.Property(aq => aq.AssignmentStructId)
|
||||
.HasColumnName("group_id")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(aq => aq.IsDeleted)
|
||||
.HasColumnName("deleted")
|
||||
.HasDefaultValue(false); // 适用于软删除策略
|
||||
@@ -60,17 +54,11 @@ namespace TechHelper.Context.Configuration
|
||||
.HasForeignKey(aq => aq.QuestionId) // 外键是 AssignmentQuestion.QuestionId
|
||||
.OnDelete(DeleteBehavior.Cascade); // 当 Question 被删除时,相关的 AssignmentQuestion 也级联删除。
|
||||
|
||||
// ---
|
||||
// 配置 AssignmentQuestion 到 AssignmentGroup 的关系 (多对一)
|
||||
// 一个 AssignmentQuestion 属于一个 AssignmentGroup。
|
||||
//
|
||||
// 你的 `AssignmentQuestion` 类现在有了 `public AssignmentGroup AssignmentGroup { get; set; }`
|
||||
// 这是一个非常好的改进,它与 `AssignmentGroupId` 外键完美匹配。
|
||||
// 假设 `AssignmentGroup` 实体中有一个名为 `AssignmentQuestions` 的 `ICollection<AssignmentQuestion>` 集合属性。
|
||||
builder.HasOne(aq => aq.AssignmentStruct) // 当前 AssignmentQuestion 有一个 AssignmentGroup
|
||||
.WithMany(ag => ag.AssignmentQuestions) // 那个 AssignmentGroup 可以有多个 AssignmentQuestion
|
||||
.HasForeignKey(aq => aq.AssignmentStructId) // 外键是 AssignmentQuestion.AssignmentGroupId (列名 detail_id)
|
||||
.OnDelete(DeleteBehavior.Cascade); // 当 AssignmentGroup 被删除时,相关的 AssignmentQuestion 也级联删除。
|
||||
|
||||
builder.HasOne(aq => aq.QuestionContext)
|
||||
.WithMany(qc => qc.Questions)
|
||||
.HasForeignKey(aq => aq.QuestionContextId)
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
|
||||
// ---
|
||||
// 配置 AssignmentQuestion 到 SubmissionDetail 的关系 (一对多)
|
||||
|
@@ -111,11 +111,6 @@ namespace TechHelper.Context.Configuration
|
||||
.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