using maintenance.Data.Entities; using Microsoft.EntityFrameworkCore; namespace maintenance.Data; public sealed class AppDbContext : DbContext { public AppDbContext(DbContextOptions options) : base(options) { } public DbSet Templates => Set(); public DbSet AppMappings => Set(); public DbSet FeatureFlags => Set(); public DbSet MaintenanceWindows => Set(); public DbSet AdminGroups => Set(); public DbSet AuthorizationGroups => Set(); public DbSet PermissionRules => Set(); public DbSet AuditRecords => Set(); public DbSet ManagedPages => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(e => { e.ToTable("Template"); e.HasKey(x => x.Id); e.Property(x => x.Name).HasMaxLength(50).IsRequired(); e.Property(x => x.Title).HasMaxLength(200).IsRequired(); e.Property(x => x.LastUpdatedBy).HasMaxLength(200).IsRequired(); e.HasIndex(x => x.Name).IsUnique(); }); modelBuilder.Entity(e => { e.ToTable("AppMapping"); e.HasKey(x => x.Id); e.Property(x => x.AppId).HasMaxLength(100).IsRequired(); e.Property(x => x.DisplayName).HasMaxLength(200).IsRequired(); e.Property(x => x.DefaultTemplate).HasMaxLength(50).IsRequired(); e.HasIndex(x => x.AppId).IsUnique(); }); modelBuilder.Entity(e => { e.ToTable("FeatureFlag"); e.HasKey(x => x.Id); e.Property(x => x.Key).HasMaxLength(100).IsRequired(); e.Property(x => x.Scope).HasMaxLength(50).IsRequired(); e.HasIndex(x => new { x.Key, x.Scope }).IsUnique(); }); modelBuilder.Entity(e => { e.ToTable("MaintenanceWindow"); e.HasKey(x => x.Id); e.Property(x => x.AppId).HasMaxLength(100); e.HasIndex(x => new { x.AppId, x.StartTimeEastern, x.EndTimeEastern }); }); modelBuilder.Entity(e => { e.ToTable("AdminGroup"); e.HasKey(x => x.Id); e.Property(x => x.GroupClaimValue).HasMaxLength(256).IsRequired(); e.Property(x => x.NormalizedGroupClaimValue).HasMaxLength(256).IsRequired(); e.HasIndex(x => x.NormalizedGroupClaimValue).IsUnique(); }); modelBuilder.Entity(e => { e.ToTable("AuthorizationGroup"); e.HasKey(x => x.Id); e.Property(x => x.GroupClaimValue).HasMaxLength(256).IsRequired(); e.Property(x => x.Role).HasMaxLength(50).IsRequired(); e.HasIndex(x => x.GroupClaimValue).IsUnique(); }); modelBuilder.Entity(e => { e.ToTable("PermissionRule"); e.HasKey(x => x.Id); e.Property(x => x.PermissionKey).HasMaxLength(200).IsRequired(); e.Property(x => x.Effect).HasMaxLength(10).IsRequired(); e.HasOne(x => x.Group) .WithMany() .HasForeignKey(x => x.GroupId) .OnDelete(DeleteBehavior.Cascade); e.HasIndex(x => new { x.PermissionKey, x.GroupId }).IsUnique(); }); modelBuilder.Entity(e => { e.ToTable("AuditRecord"); e.HasKey(x => x.Id); e.Property(x => x.Actor).HasMaxLength(200).IsRequired(); e.Property(x => x.Action).HasMaxLength(100).IsRequired(); e.Property(x => x.Target).HasMaxLength(200).IsRequired(); e.HasIndex(x => x.TimestampUtc); }); modelBuilder.Entity(e => { e.ToTable("ManagedPage"); e.HasKey(x => x.Id); e.Property(x => x.Key).HasMaxLength(100).IsRequired(); e.Property(x => x.Stub).HasMaxLength(100); e.Property(x => x.NormalizedStub).HasMaxLength(100); e.Property(x => x.DisplayName).HasMaxLength(200).IsRequired(); e.Property(x => x.PageKind).HasMaxLength(50).IsRequired(); e.Property(x => x.RecurrenceRule).HasMaxLength(200); e.Property(x => x.LastUpdatedBy).HasMaxLength(200).IsRequired(); e.Property(x => x.ContentJson).IsRequired(); e.HasIndex(x => x.Key).IsUnique(); e.HasIndex(x => new { x.PageKind, x.NormalizedStub }) .IsUnique() .HasFilter("[NormalizedStub] IS NOT NULL"); e.HasIndex(x => new { x.PageKind, x.IsEnabled, x.Priority }); }); } }