43 lines
1.9 KiB
C#
43 lines
1.9 KiB
C#
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 });
|
|
});
|
|
}
|
|
}
|