/****************************************************************************** // RomRepoMgr - ROM repository manager // ---------------------------------------------------------------------------- // // Author(s) : Natalia Portillo // // --[ License ] -------------------------------------------------------------- // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // ---------------------------------------------------------------------------- // Copyright © 2020-2024 Natalia Portillo *******************************************************************************/ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using RomRepoMgr.Database.Models; namespace RomRepoMgr.Database; public sealed class Context(DbContextOptions options) : DbContext(options) { public DbSet Files { get; set; } public DbSet RomSets { get; set; } public DbSet Machines { get; set; } public DbSet FilesByMachines { get; set; } public DbSet Disks { get; set; } public DbSet DisksByMachines { get; set; } public DbSet Medias { get; set; } public DbSet MediasByMachines { get; set; } public DbSet RomSetStats { get; set; } public static Context Create(string dbPath, ILoggerFactory loggerFactory) { var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseLazyLoadingProxies() #if DEBUG .UseLoggerFactory(loggerFactory) #endif .UseSqlite($"Data Source={dbPath}"); return new Context(optionsBuilder.Options); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity(entity => { entity.HasIndex(e => e.Crc32); entity.HasIndex(e => e.Md5); entity.HasIndex(e => e.Sha1); entity.HasIndex(e => e.Sha256); entity.HasIndex(e => e.Sha384); entity.HasIndex(e => e.Sha512); entity.HasIndex(e => e.Size); entity.HasIndex(e => e.IsInRepo); entity.HasIndex(e => new { e.Crc32, e.Size }); entity.HasIndex(e => new { e.Md5, e.Size }); entity.HasIndex(e => new { e.Sha1, e.Size }); entity.HasIndex(e => new { e.Sha256, e.Size }); entity.HasIndex(e => new { e.Sha384, e.Size }); entity.HasIndex(e => new { e.Sha512, e.Size }); }); modelBuilder.Entity(entity => { entity.HasIndex(e => e.Author); entity.HasIndex(e => e.Comment); entity.HasIndex(e => e.Date); entity.HasIndex(e => e.Description); entity.HasIndex(e => e.Homepage); entity.HasIndex(e => e.Name); entity.HasIndex(e => e.Version); entity.HasIndex(e => e.Filename); entity.HasIndex(e => e.Sha384); entity.HasIndex(e => e.Category); }); modelBuilder.Entity(entity => { entity.HasIndex(e => e.Name); entity.HasOne(e => e.RomSet).WithMany(e => e.Machines).OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity(entity => { entity.HasIndex(e => e.Name); entity.HasOne(e => e.Machine).WithMany(e => e.Files).OnDelete(DeleteBehavior.Cascade); entity.HasOne(e => e.File).WithMany(e => e.Machines).OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity(entity => { entity.HasIndex(e => e.Md5); entity.HasIndex(e => e.Sha1); entity.HasIndex(e => e.Size); entity.HasIndex(e => e.IsInRepo); }); modelBuilder.Entity(entity => { entity.HasIndex(e => e.Name); entity.HasOne(e => e.Machine).WithMany(e => e.Disks).OnDelete(DeleteBehavior.Cascade); entity.HasOne(e => e.Disk).WithMany(e => e.Machines).OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity(entity => { entity.HasIndex(e => e.Md5); entity.HasIndex(e => e.Sha1); entity.HasIndex(e => e.Sha256); entity.HasIndex(e => e.SpamSum); entity.HasIndex(e => e.Size); entity.HasIndex(e => e.IsInRepo); }); modelBuilder.Entity(entity => { entity.HasIndex(e => e.Name); entity.HasOne(e => e.Machine).WithMany(e => e.Medias).OnDelete(DeleteBehavior.Cascade); entity.HasOne(e => e.Media).WithMany(e => e.Machines).OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity(entity => entity.HasOne(e => e.RomSet).WithOne(e => e.Statistics)); } }