Remove database singleton.

This commit is contained in:
2020-09-05 02:49:41 +01:00
parent 9818cd43c6
commit 49c69b9524
8 changed files with 118 additions and 133 deletions

View File

@@ -23,37 +23,16 @@
// Copyright © 2020 Natalia Portillo
*******************************************************************************/
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using RomRepoMgr.Database.Models;
using RomRepoMgr.Database.Resources;
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),
Localization.Settings_not_initialized);
_singleton = Create(Settings.Settings.Current.DatabasePath);
return _singleton;
}
}
public DbSet<DbFile> Files { get; set; }
public DbSet<RomSet> RomSets { get; set; }
public DbSet<Machine> Machines { get; set; }
@@ -63,8 +42,6 @@ namespace RomRepoMgr.Database
public DbSet<DbMedia> Medias { get; set; }
public DbSet<MediaByMachine> MediasByMachines { get; set; }
public static void ReplaceSingleton(string dbPath) => _singleton = Create(dbPath);
public static Context Create(string dbPath)
{
var optionsBuilder = new DbContextOptionsBuilder();