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"); + + // 關聯與刪除行為 + 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() + .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