Files
TechHelper/TechHelper.Server/Context/Configuration/AssignmentConfiguration.cs
2025-05-23 19:03:00 +08:00

83 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Entities.Contracts;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace TechHelper.Context.Configuration
{
public class AssignmentConfiguration : IEntityTypeConfiguration<Assignment>
{
public void Configure(EntityTypeBuilder<Assignment> builder)
{
builder.ToTable("assignments");
builder.HasKey(a => a.Id);
builder.Property(a => a.Id)
.HasColumnName("id")
.ValueGeneratedOnAdd();
builder.Property(a => a.Title)
.IsRequired()
.HasColumnName("title")
.HasMaxLength(255);
builder.Property(a => a.Description)
.HasColumnName("description");
builder.Property(a => a.DueDate)
.IsRequired()
.HasColumnName("due_date");
builder.Property(a => a.TotalPoints)
.HasColumnName("total_points");
builder.Property(a => a.CreatedBy)
.HasColumnName("created_by");
builder.Property(a => a.CreatedAt)
.IsRequired()
.HasColumnName("created_at");
builder.Property(a => a.UpdatedAt)
.IsRequired()
.HasColumnName("updated_at");
builder.Property(a => a.IsDeleted)
.HasColumnName("deleted");
// 配置导航属性和关系
// 关系: Assignment (多) 到 User (一) - CreatedBy 外键
// 假设 User 实体有 Id 作为主键
// .WithMany() 表示 User 有多个 Assignment但这里不指定 User 上的导航属性名称
// 如果 User 有一个名为 AssignmentsCreated 的导航属性,应写为 .WithMany(u => u.AssignmentsCreated)
builder.HasOne(a => a.Creator)
.WithMany() // User 实体没有指向 Assignment 的导航属性 (或我们不知道)
.HasForeignKey(a => a.CreatedBy)
.IsRequired(); // CreatedBy 是必填的,对应 [Required] 在外键属性上
// 关系: Assignment (一) 到 AssignmentClass (多)
// 假设 AssignmentClass 实体包含一个名为 AssignmentId 的外键属性
builder.HasMany(a => a.AssignmentClasses)
.WithOne(ac => ac.Assignment) // AssignmentClass 没有指向 Assignment 的导航属性 (或我们不知道)
.HasForeignKey("AssignmentId") // 指定外键名称为 AssignmentId
.OnDelete(DeleteBehavior.Cascade); // 如果 Assignment 被删除,关联的 AssignmentClass 也会被删除
// 关系: Assignment (一) 到 AssignmentAttachment (多)
// 假设 AssignmentAttachment 实体包含一个名为 AssignmentId 的外键属性
builder.HasMany(a => a.AssignmentAttachments)
.WithOne(aa => aa.Assignment)
.HasForeignKey("AssignmentId")
.OnDelete(DeleteBehavior.Cascade);
// 关系: Assignment (一) 到 Submission (多)
// 假设 Submission 实体包含一个名为 AssignmentId 的外键属性
builder.HasMany(a => a.Submissions)
.WithOne(s => s.Assignment)
.HasForeignKey("AssignmentId")
.OnDelete(DeleteBehavior.Cascade);
}
}
}