using Microsoft.EntityFrameworkCore; using HyperBooru.Services; namespace HyperBooru; public class HBContext : DbContext { public static readonly Guid NsfwTag = new("EBDAD4F8-455A-4351-8017-1D4854D6FA38"); public static readonly Guid IngestTag = new("EA212801-5BCC-4C0E-814F-FB9D30DB58BC"); public DbSet Objects { get; set; } public DbSet TagDefinitions { get; set; } public DbSet Tags { get; set; } public DbSet Media { get; set; } public DbSet UploadedFiles { get; set; } private IConfigService config; public HBContext(DbContextOptions options, IConfigService config) : base(options) => this.config = config; protected override void OnConfiguring(DbContextOptionsBuilder options) { options.UseLazyLoadingProxies(); options.UseNpgsql(config.DbConnectionString); #if DEBUG options.EnableSensitiveDataLogging(); #endif } protected override void OnModelCreating(ModelBuilder modelBuilder) { // Don't use shared tables for inherited types modelBuilder.Entity().ToTable("Objects"); modelBuilder.Entity().ToTable("TagDefinitions"); modelBuilder.Entity().ToTable("Tags"); modelBuilder.Entity().ToTable("Media"); modelBuilder.Entity().ToTable("UploadedFiles"); // Seed internal tag definitions // These should NEVER change modelBuilder.Entity().HasData(new TagDefinition[] { new() { ObjectId = -1, Guid = NsfwTag, Source = TagSource.Internal, Name = "nsfw" }, new() { ObjectId = -2, Guid = IngestTag, Source = TagSource.Internal, Name = "ingest" } }); // Implicit tags need some special attention to make many<->many // navigations work for the same object type. modelBuilder.Entity() .HasMany(e => e.ImplicitTags) .WithMany(); } }