Files
romrepomgr/RomRepoMgr.Database/Context.cs

111 lines
3.6 KiB
C#
Raw Normal View History

2020-08-21 23:32:44 +01:00
/******************************************************************************
// RomRepoMgr - ROM repository manager
2020-08-21 23:21:01 +01:00
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ License ] --------------------------------------------------------------
//
2020-08-21 23:32:44 +01:00
// 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
2020-08-21 23:21:01 +01:00
// License, or (at your option) any later version.
//
2020-08-21 23:32:44 +01:00
// 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.
2020-08-21 23:21:01 +01:00
//
2020-08-21 23:32:44 +01:00
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2020-08-21 23:21:01 +01:00
//
// ----------------------------------------------------------------------------
2020-08-21 23:32:44 +01:00
// Copyright © 2020 Natalia Portillo
*******************************************************************************/
2020-08-21 23:21:01 +01:00
2020-08-22 02:39:44 +01:00
using System;
2020-08-21 23:21:01 +01:00
using Microsoft.EntityFrameworkCore;
2020-08-21 23:32:44 +01:00
using RomRepoMgr.Database.Models;
2020-08-21 23:21:01 +01:00
namespace RomRepoMgr.Database
{
public sealed class Context : DbContext
{
2020-08-22 02:39:44 +01:00
static Context _singleton;
2020-08-21 23:21:01 +01:00
public Context(DbContextOptions options) : base(options) {}
2020-08-22 02:39:44 +01:00
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;
}
}
2020-08-22 14:20:59 +01:00
public DbSet<DbFile> Files { get; set; }
public DbSet<RomSet> RomSets { get; set; }
public DbSet<Machine> Machines { get; set; }
2020-08-21 23:32:44 +01:00
2020-08-21 23:21:01 +01:00
public static Context Create(string dbPath)
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseLazyLoadingProxies().UseSqlite($"Data Source={dbPath}");
return new Context(optionsBuilder.Options);
}
2020-08-21 23:32:44 +01:00
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<DbFile>(entity =>
{
entity.HasIndex(e => e.Crc32);
entity.HasIndex(e => e.Sha1);
entity.HasIndex(e => e.Sha256);
entity.HasIndex(e => e.Size);
});
2020-08-22 05:40:50 +01:00
modelBuilder.Entity<RomSet>(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);
});
2020-08-22 14:20:59 +01:00
modelBuilder.Entity<Machine>(entity =>
{
entity.HasIndex(e => e.Name);
entity.HasOne(e => e.RomSet).WithMany(e => e.Machines).OnDelete(DeleteBehavior.Cascade);
});
2020-08-21 23:32:44 +01:00
}
2020-08-21 23:21:01 +01:00
}
}