From 194609720a53b98a5b7ebe6dd7a7f64e8fa7f548 Mon Sep 17 00:00:00 2001 From: Chunyi Date: Fri, 26 Sep 2025 16:31:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AF=A6=E9=AB=94=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=BB=A5=E5=AE=9A=E7=BE=A9=E8=B3=87=E6=96=99=E5=BA=AB?= =?UTF-8?q?=E7=B5=90=E6=A7=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 這些變更為 `Account`、`Department` 和 `Role` 實體配置資料庫結構。每個配置類別實作了 `IEntityTypeConfiguration` 介面,並在 `Configure` 方法中定義了主鍵、欄位名稱、必填性和最大長度等屬性。此外,為 `Account` 實體設置了索引和外鍵關聯,以確保資料的完整性和唯一性。 --- Configurations/AccountConfiguration.cs | 89 +++++++++++++++++++++++ Configurations/DepartmentConfiguration.cs | 24 ++++++ Configurations/RoleConfiguration.cs | 24 ++++++ 3 files changed, 137 insertions(+) create mode 100644 Configurations/AccountConfiguration.cs create mode 100644 Configurations/DepartmentConfiguration.cs create mode 100644 Configurations/RoleConfiguration.cs diff --git a/Configurations/AccountConfiguration.cs b/Configurations/AccountConfiguration.cs new file mode 100644 index 0000000..7137c09 --- /dev/null +++ b/Configurations/AccountConfiguration.cs @@ -0,0 +1,89 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Models.Entities; + +namespace Configurations +{ + public class AccountConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder 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() + .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"); + + // pPR欰 + 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() + .WithMany() + .HasForeignKey(a => a.ModifiedBy) + .OnDelete(DeleteBehavior.SetNull); + } + } +} \ No newline at end of file diff --git a/Configurations/DepartmentConfiguration.cs b/Configurations/DepartmentConfiguration.cs new file mode 100644 index 0000000..4f92650 --- /dev/null +++ b/Configurations/DepartmentConfiguration.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Models.Entities; + +namespace Configurations +{ + public class DepartmentConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder 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); + } + } +} \ No newline at end of file diff --git a/Configurations/RoleConfiguration.cs b/Configurations/RoleConfiguration.cs new file mode 100644 index 0000000..d4b6588 --- /dev/null +++ b/Configurations/RoleConfiguration.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Models.Entities; + +namespace Configurations +{ + public class RoleConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder 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); + } + } +} \ No newline at end of file