新增實體配置以定義資料庫結構

這些變更為 `Account`、`Department` 和 `Role` 實體配置資料庫結構。每個配置類別實作了 `IEntityTypeConfiguration<T>` 介面,並在 `Configure` 方法中定義了主鍵、欄位名稱、必填性和最大長度等屬性。此外,為 `Account` 實體設置了索引和外鍵關聯,以確保資料的完整性和唯一性。
This commit is contained in:
Chen, Chun-Yi 2025-09-26 16:31:56 +08:00
parent 171a60089b
commit 194609720a
3 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,89 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Models.Entities;
namespace Configurations
{
public class AccountConfiguration : IEntityTypeConfiguration<Account>
{
public void Configure(EntityTypeBuilder<Account> builder)
{
builder.ToTable("accounts");
builder.HasKey(a => a.AccountId);
builder.Property(a => a.AccountId)
.HasColumnName("account_id");
builder.Property(a => a.Name)
.HasColumnName("name")
.IsRequired()
.HasMaxLength(100);
builder.Property(a => a.Username)
.HasColumnName("username")
.IsRequired()
.HasMaxLength(50);
builder.Property(a => a.Password)
.HasColumnName("password")
.IsRequired()
.HasMaxLength(256);
builder.Property(a => a.Email)
.HasColumnName("email")
.IsRequired()
.HasMaxLength(254);
builder.Property(a => a.DepartmentId)
.HasColumnName("department_id")
.IsRequired();
builder.Property(a => a.RoleId)
.HasColumnName("role_id")
.IsRequired();
builder.Property(a => a.Status)
.HasColumnName("status")
.HasConversion<string>()
.IsRequired()
.HasMaxLength(20);
builder.Property(a => a.CreatedAt)
.HasColumnName("created_at")
.IsRequired();
builder.Property(a => a.UpdatedAt)
.HasColumnName("updated_at");
builder.Property(a => a.ModifiedBy)
.HasColumnName("modified_by");
// 唯一索引
builder.HasIndex(a => a.Username)
.IsUnique()
.HasDatabaseName("UX_accounts_username");
builder.HasIndex(a => a.Email)
.IsUnique()
.HasDatabaseName("UX_accounts_email");
// 關聯與刪除行為
builder.HasOne(a => a.Department)
.WithMany(d => d.Accounts)
.HasForeignKey(a => a.DepartmentId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(a => a.Role)
.WithMany(r => r.Accounts)
.HasForeignKey(a => a.RoleId)
.OnDelete(DeleteBehavior.Restrict);
// 自參照外鍵modified_by -> accounts.account_id (SET NULL)
builder.HasOne<Account>()
.WithMany()
.HasForeignKey(a => a.ModifiedBy)
.OnDelete(DeleteBehavior.SetNull);
}
}
}

View File

@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Models.Entities;
namespace Configurations
{
public class DepartmentConfiguration : IEntityTypeConfiguration<Department>
{
public void Configure(EntityTypeBuilder<Department> builder)
{
builder.ToTable("departments");
builder.HasKey(d => d.DepartmentId);
builder.Property(d => d.DepartmentId)
.HasColumnName("department_id");
builder.Property(d => d.DepartmentName)
.HasColumnName("department_name")
.IsRequired()
.HasMaxLength(100);
}
}
}

View File

@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Models.Entities;
namespace Configurations
{
public class RoleConfiguration : IEntityTypeConfiguration<Role>
{
public void Configure(EntityTypeBuilder<Role> builder)
{
builder.ToTable("roles");
builder.HasKey(r => r.RoleId);
builder.Property(r => r.RoleId)
.HasColumnName("role_id");
builder.Property(r => r.RoleName)
.HasColumnName("role_name")
.IsRequired()
.HasMaxLength(50);
}
}
}