thecr3w-dev/backend/TheCr3w.Shared/Data/TheCr3wDbContext.cs

55 lines
2.2 KiB
C#

using Microsoft.EntityFrameworkCore;
namespace TheCr3w.Shared.Data;
public class TheCr3wDbContext : DbContext
{
public TheCr3wDbContext(DbContextOptions<TheCr3wDbContext> options) : base(options)
{
}
public DbSet<ApplicationEntity> Applications => Set<ApplicationEntity>();
public DbSet<ApplicationPlatformEntity> ApplicationPlatforms => Set<ApplicationPlatformEntity>();
public DbSet<ReleaseEntity> Releases => Set<ReleaseEntity>();
public DbSet<InstallationReportEntity> InstallationReports => Set<InstallationReportEntity>();
public DbSet<ErrorReportEntity> ErrorReports => Set<ErrorReportEntity>();
public DbSet<DeploymentManifestEntity> DeploymentManifests => Set<DeploymentManifestEntity>();
public DbSet<DeploymentApprovalEntity> DeploymentApprovals => Set<DeploymentApprovalEntity>();
public DbSet<AuditEventEntity> AuditEvents => Set<AuditEventEntity>();
public DbSet<ExternalServerEntity> ExternalServers => Set<ExternalServerEntity>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Złożony klucz główny dla application_platforms (app_id, platform)
modelBuilder.Entity<ApplicationPlatformEntity>()
.HasKey(ap => new { ap.AppId, ap.Platform });
modelBuilder.Entity<ApplicationPlatformEntity>()
.HasOne(ap => ap.Application)
.WithMany(a => a.Platforms)
.HasForeignKey(ap => ap.AppId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ReleaseEntity>()
.HasOne(r => r.Application)
.WithMany(a => a.Releases)
.HasForeignKey(r => r.AppId)
.OnDelete(DeleteBehavior.Restrict);
// Indeksy wydajnościowe
modelBuilder.Entity<ReleaseEntity>()
.HasIndex(r => new { r.AppId, r.Platform, r.Architecture, r.Channel, r.Status, r.VersionCode });
modelBuilder.Entity<AuditEventEntity>()
.HasIndex(a => new { a.ResourceType, a.ResourceId });
modelBuilder.Entity<AuditEventEntity>()
.HasIndex(a => a.CreatedAt);
modelBuilder.Entity<DeploymentManifestEntity>()
.HasIndex(m => new { m.EnvironmentId, m.Status });
}
}