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 options) : DbContext(options) { public DbSet Users => Set(); public DbSet OrganizationUnits => Set(); public DbSet AuditEntries => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(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(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().WithMany().HasForeignKey(x => x.ParentId).OnDelete(DeleteBehavior.Restrict); }); modelBuilder.Entity(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 }); }); } }