This commit is contained in:
biss committed 2026-08-29 11:24:05 +08:00
1 parent a1f48945e8
commit ab935480b0
26 files changed
+829 -5

No files matched your search

@@ -0,0 +1,42 @@
using ElectionSystem.Api.Infrastructure.Auditing;
using ElectionSystem.Api.Modules.Identity.Domain;
using ElectionSystem.Api.Modules.Organization.Domain;
using Microsoft.EntityFrameworkCore;
namespace ElectionSystem.Api.Infrastructure.Persistence;
public sealed class ElectionDbContext(DbContextOptions<ElectionDbContext> options) : DbContext(options)
{
public DbSet<ApplicationUser> Users => Set<ApplicationUser>();
public DbSet<OrganizationUnit> OrganizationUnits => Set<OrganizationUnit>();
public DbSet<AuditEntry> AuditEntries => Set<AuditEntry>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>(entity =>
{
entity.ToTable("identity_users");
entity.Property(x => x.UserName).HasMaxLength(128).IsRequired();
entity.Property(x => x.DisplayName).HasMaxLength(128).IsRequired();
entity.HasIndex(x => x.UserName).IsUnique();
});
modelBuilder.Entity<OrganizationUnit>(entity =>
{
entity.ToTable("organization_units");
entity.Property(x => x.Name).HasMaxLength(200).IsRequired();
entity.Property(x => x.Code).HasMaxLength(64).IsRequired();
entity.HasIndex(x => x.Code).IsUnique();
entity.HasOne<OrganizationUnit>().WithMany().HasForeignKey(x => x.ParentId).OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity<AuditEntry>(entity =>
{
entity.ToTable("audit_entries");
entity.Property(x => x.Action).HasMaxLength(128).IsRequired();
entity.Property(x => x.ResourceType).HasMaxLength(128).IsRequired();
entity.Property(x => x.TraceId).HasMaxLength(128);
entity.Property(x => x.ActorId).HasMaxLength(128);
entity.Property(x => x.MetadataJson).HasColumnType("json");
entity.HasIndex(x => new { x.OccurredAtUtc, x.ResourceType });
});
}
}