XisongSpaceBooking_BackEnd/Configurations/AccountConfiguration.cs
Chunyi 194609720a 新增實體配置以定義資料庫結構
這些變更為 `Account`、`Department` 和 `Role` 實體配置資料庫結構。每個配置類別實作了 `IEntityTypeConfiguration<T>` 介面,並在 `Configure` 方法中定義了主鍵、欄位名稱、必填性和最大長度等屬性。此外,為 `Account` 實體設置了索引和外鍵關聯,以確保資料的完整性和唯一性。
2025-09-26 16:31:56 +08:00

89 lines
2.7 KiB
C#

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");
// ÃöÁp»P§R°£¦æ¬°
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);
// ¦Û°Ñ·Ó¥~Áä¡Gmodified_by -> accounts.account_id (SET NULL)
builder.HasOne<Account>()
.WithMany()
.HasForeignKey(a => a.ModifiedBy)
.OnDelete(DeleteBehavior.SetNull);
}
}
}