添加项目文件。
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user