/****************************************************************************** // 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 Natalia Portillo *******************************************************************************/ using System; using Microsoft.EntityFrameworkCore; using RomRepoMgr.Database.Models; namespace RomRepoMgr.Database { public sealed class Context : DbContext { static Context _singleton; public Context(DbContextOptions options) : base(options) {} public static Context Singleton { get { if(_singleton != null) return _singleton; if(Settings.Settings.Current?.DatabasePath is null) throw new ArgumentNullException(nameof(Settings.Settings.Current.DatabasePath), "Settings are not initialized!"); _singleton = Create(Settings.Settings.Current.DatabasePath); return _singleton; } } public DbSet Files { get; set; } public static Context Create(string dbPath) { var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseLazyLoadingProxies().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.Sha1); entity.HasIndex(e => e.Sha256); entity.HasIndex(e => e.Size); }); } } }