83 lines
4.0 KiB
C#
83 lines
4.0 KiB
C#
using Entities.Contracts;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace TechHelper.Context.Configuration
|
|
{
|
|
public class AssignmentGroupConfiguration : IEntityTypeConfiguration<AssignmentGroup>
|
|
{
|
|
public void Configure(EntityTypeBuilder<AssignmentGroup> 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.Descript)
|
|
.HasColumnName("descript")
|
|
.HasMaxLength(65535); // 对应 MaxLength(65535)
|
|
|
|
// 配置 TotalPoints 属性对应的数据库列名为 "total_points"。
|
|
// TotalPoints 是 decimal? 类型,默认就是可选的,无需 IsRequired(false)。
|
|
builder.Property(ag => ag.TotalPoints)
|
|
.HasColumnName("total_points");
|
|
|
|
// 配置 Number 属性对应的数据库列名为 "number"。
|
|
builder.Property(ag => ag.Number)
|
|
.HasColumnName("number")
|
|
.IsRequired(); // byte 默认非空,显式 IsRequired 增加可读性。
|
|
|
|
// 配置 ParentGroup 属性对应的数据库列名为 "sub_group"。
|
|
// ParentGroup 是 Guid? 类型,默认就是可选的,无需 IsRequired(false)。
|
|
builder.Property(ag => ag.ParentGroup)
|
|
.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
|
|
.WithMany(a => a.AssignmentGroups) // 该 Assignment 可以有多个 AssignmentGroup 记录
|
|
.HasForeignKey(ag => ag.AssignmentId) // 通过 AssignmentId 建立外键
|
|
.OnDelete(DeleteBehavior.Cascade); // 当关联的 Assignment 被删除时,其所有相关的 AssignmentGroup 记录也级联删除。
|
|
|
|
// 配置 AssignmentGroup 到 AssignmentGroup 的自引用关系(父子关系)。
|
|
// 一个 AssignmentGroup 可以有一个父 AssignmentGroup (SubAssignmentGroup)。
|
|
// 假设父 AssignmentGroup 实体中有一个名为 ChildAssignmentGroups 的集合属性来表示它所包含的所有子组。
|
|
builder.HasOne(ag => ag.ParentAssignmentGroup) // 当前 AssignmentGroup 有一个父 AssignmentGroup
|
|
.WithMany(parentAg => parentAg.ChildAssignmentGroups) // 该父 AssignmentGroup 可以有多个子 AssignmentGroup
|
|
.HasForeignKey(ag => ag.ParentGroup) // 通过 SubGroup 建立外键
|
|
.IsRequired(false) // SubGroup 是可空的 (Guid?),所以这个关系是可选的。
|
|
.OnDelete(DeleteBehavior.SetNull); // 当父 AssignmentGroup 被删除时,其子 AssignmentGroup 的 SubGroup 外键将被设置为 NULL。
|
|
// 如果你希望父组被删除时子组不能脱离父组(即不允许父组被删除),
|
|
// 可以使用 DeleteBehavior.Restrict 或 DeleteBehavior.NoAction。
|
|
}
|
|
}
|
|
} |