2026-07-16 20:53:56 +00:00
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using Heimdall.Infrastructure.Data.Interceptors;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-07-18 20:31:37 +00:00
|
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
2026-07-16 20:53:56 +00:00
|
|
|
|
|
|
|
|
namespace Heimdall.Infrastructure.Data;
|
|
|
|
|
|
|
|
|
|
public class AppDbContext : IdentityDbContext<AppUser, AppRole, Guid>
|
|
|
|
|
{
|
|
|
|
|
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
|
|
|
|
|
|
|
|
|
|
// Domain entities
|
|
|
|
|
public DbSet<Site> Sites => Set<Site>();
|
|
|
|
|
public DbSet<Agent> Agents => Set<Agent>();
|
|
|
|
|
public DbSet<Camera> Cameras => Set<Camera>();
|
|
|
|
|
public DbSet<CameraCredentials> CameraCredentials => Set<CameraCredentials>();
|
|
|
|
|
public DbSet<CameraDetectionProfile> CameraDetectionProfiles => Set<CameraDetectionProfile>();
|
|
|
|
|
public DbSet<UserSite> UserSites => Set<UserSite>();
|
|
|
|
|
public DbSet<ApiKey> ApiKeys => Set<ApiKey>();
|
|
|
|
|
public DbSet<AuditLog> AuditLogs => Set<AuditLog>();
|
2026-07-18 18:58:20 +00:00
|
|
|
public DbSet<AppSetting> AppSettings => Set<AppSetting>();
|
|
|
|
|
public DbSet<AgentSetting> AgentSettings => Set<AgentSetting>();
|
2026-07-16 20:53:56 +00:00
|
|
|
|
2026-07-18 21:15:05 +00:00
|
|
|
// Discovery entities
|
|
|
|
|
public DbSet<CameraDiscoveryResult> CameraDiscoveryResults => Set<CameraDiscoveryResult>();
|
|
|
|
|
public DbSet<DiscoveredStreamUrl> DiscoveredStreams => Set<DiscoveredStreamUrl>();
|
|
|
|
|
|
2026-07-16 20:53:56 +00:00
|
|
|
protected override void OnModelCreating(ModelBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
base.OnModelCreating(builder);
|
|
|
|
|
|
|
|
|
|
builder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
|
|
|
|
|
|
2026-07-18 20:31:37 +00:00
|
|
|
// Global soft-delete filter — principal entities
|
2026-07-16 20:53:56 +00:00
|
|
|
builder.Entity<Site>().HasQueryFilter(e => !e.IsDeleted);
|
|
|
|
|
builder.Entity<Agent>().HasQueryFilter(e => !e.IsDeleted);
|
|
|
|
|
builder.Entity<Camera>().HasQueryFilter(e => !e.IsDeleted);
|
2026-07-18 20:31:37 +00:00
|
|
|
|
|
|
|
|
// Matching query filters on dependent entities to prevent
|
|
|
|
|
// "required navigation with query filter" EF Core warnings.
|
|
|
|
|
// Without these, loading an AgentSetting whose Agent was soft-deleted
|
|
|
|
|
// would cause a null reference on the required navigation.
|
|
|
|
|
builder.Entity<AgentSetting>().HasQueryFilter(e => e.Agent == null || !e.Agent.IsDeleted);
|
|
|
|
|
builder.Entity<CameraCredentials>().HasQueryFilter(e => e.Camera == null || !e.Camera.IsDeleted);
|
|
|
|
|
builder.Entity<CameraDetectionProfile>().HasQueryFilter(e => e.Camera == null || !e.Camera.IsDeleted);
|
|
|
|
|
builder.Entity<UserSite>().HasQueryFilter(e => e.Site == null || !e.Site.IsDeleted);
|
2026-07-16 20:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
|
{
|
2026-07-18 20:05:22 +00:00
|
|
|
optionsBuilder.AddInterceptors(
|
|
|
|
|
new AuditInterceptor(),
|
|
|
|
|
new SoftDeleteInterceptor());
|
2026-07-16 20:53:56 +00:00
|
|
|
|
2026-07-18 20:31:37 +00:00
|
|
|
// Suppress informational EF Core messages that create noise in
|
|
|
|
|
// error tracking (GlitchTip #538). Redundant index warnings are
|
|
|
|
|
// automatically handled by EF Core — it simply skips creating FK
|
|
|
|
|
// indexes that are already covered by composite indexes/PKs.
|
|
|
|
|
optionsBuilder.ConfigureWarnings(warnings =>
|
|
|
|
|
{
|
|
|
|
|
warnings.Ignore(CoreEventId.RedundantIndexRemoved);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-16 20:53:56 +00:00
|
|
|
base.OnConfiguring(optionsBuilder);
|
|
|
|
|
}
|
|
|
|
|
}
|