Files
Heimdall/src/Heimdall.Infrastructure/Data/AppDbContext.cs
T

80 lines
3.6 KiB
C#
Raw Normal View History

using Heimdall.Domain.Entities;
using Heimdall.Infrastructure.Data.Interceptors;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
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>();
public DbSet<AppSetting> AppSettings => Set<AppSetting>();
public DbSet<AgentSetting> AgentSettings => Set<AgentSetting>();
public DbSet<Group> Groups => Set<Group>();
public DbSet<UserGroup> UserGroups => Set<UserGroup>();
// Discovery entities
public DbSet<CameraDiscoveryResult> CameraDiscoveryResults => Set<CameraDiscoveryResult>();
public DbSet<DiscoveredStreamUrl> DiscoveredStreams => Set<DiscoveredStreamUrl>();
// Assignment entities
public DbSet<CameraAssignment> CameraAssignments => Set<CameraAssignment>();
// Event entities
public DbSet<CameraEvent> CameraEvents => Set<CameraEvent>();
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
// Global soft-delete filter — principal entities
builder.Entity<Site>().HasQueryFilter(e => !e.IsDeleted);
builder.Entity<Agent>().HasQueryFilter(e => !e.IsDeleted);
builder.Entity<Camera>().HasQueryFilter(e => !e.IsDeleted);
// 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);
// Group soft-delete filters
builder.Entity<Group>().HasQueryFilter(e => !e.IsDeleted);
builder.Entity<UserGroup>().HasQueryFilter(e => e.Group == null || !e.Group.IsDeleted);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.AddInterceptors(
new AuditInterceptor(),
new SoftDeleteInterceptor());
// 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);
});
base.OnConfiguring(optionsBuilder);
}
}