Files
romrepomgr/RomRepoMgr.Database/Context.cs

201 lines
5.9 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
//
// ----------------------------------------------------------------------------
2024-11-08 19:13:57 +00:00
// Copyright © 2020-2024 Natalia Portillo
2020-08-21 23:32:44 +01:00
*******************************************************************************/
2020-08-21 23:21:01 +01:00
using Microsoft.EntityFrameworkCore;
2020-08-24 01:57:15 +01:00
using Microsoft.Extensions.Logging;
2020-08-21 23:32:44 +01:00
using RomRepoMgr.Database.Models;
2020-08-21 23:21:01 +01:00
2024-11-09 01:37:59 +00:00
namespace RomRepoMgr.Database;
public sealed class Context(DbContextOptions options) : DbContext(options)
2020-08-21 23:21:01 +01:00
{
2024-11-09 01:37:59 +00:00
public DbSet<DbFile> Files { get; set; }
public DbSet<RomSet> RomSets { get; set; }
public DbSet<Machine> Machines { get; set; }
public DbSet<FileByMachine> FilesByMachines { get; set; }
public DbSet<DbDisk> Disks { get; set; }
public DbSet<DiskByMachine> DisksByMachines { get; set; }
public DbSet<DbMedia> Medias { get; set; }
public DbSet<MediaByMachine> MediasByMachines { get; set; }
public DbSet<RomSetStat> RomSetStats { get; set; }
public static Context Create(string dbPath, ILoggerFactory loggerFactory)
2020-08-21 23:21:01 +01:00
{
2024-11-09 01:37:59 +00:00
var optionsBuilder = new DbContextOptionsBuilder();
2020-08-21 23:21:01 +01:00
2024-11-09 01:37:59 +00:00
optionsBuilder.UseLazyLoadingProxies()
#if DEBUG
.UseLoggerFactory(loggerFactory)
2024-11-09 01:37:59 +00:00
#endif
.UseSqlite($"Data Source={dbPath}");
2020-08-21 23:21:01 +01:00
2024-11-09 01:37:59 +00:00
return new Context(optionsBuilder.Options);
}
2020-08-21 23:32:44 +01:00
2024-11-09 01:37:59 +00:00
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
2020-08-21 23:32:44 +01:00
2024-11-09 01:37:59 +00:00
modelBuilder.Entity<DbFile>(entity =>
{
entity.HasIndex(e => e.Crc32);
2020-09-04 02:17:08 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Md5);
2020-08-21 23:32:44 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Sha1);
2020-08-21 23:32:44 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Sha256);
2020-08-22 14:52:23 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Sha384);
2020-08-22 14:52:23 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Sha512);
2020-09-04 22:27:05 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Size);
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.IsInRepo);
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => new
{
e.Crc32,
e.Size
});
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => new
{
e.Md5,
e.Size
});
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => new
{
e.Sha1,
e.Size
});
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => new
{
e.Sha256,
e.Size
});
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => new
{
e.Sha384,
e.Size
2020-08-21 23:32:44 +01:00
});
2020-08-22 05:40:50 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => new
2020-08-22 05:40:50 +01:00
{
2024-11-09 01:37:59 +00:00
e.Sha512,
e.Size
});
});
2020-08-22 05:40:50 +01:00
2024-11-09 01:37:59 +00:00
modelBuilder.Entity<RomSet>(entity =>
{
entity.HasIndex(e => e.Author);
2020-08-22 05:40:50 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Comment);
2020-08-22 05:40:50 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Date);
2020-08-22 05:40:50 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Description);
2020-08-22 05:40:50 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Homepage);
2020-08-22 05:40:50 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Name);
2020-08-22 05:40:50 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Version);
2020-08-22 05:40:50 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Filename);
2020-09-05 01:52:43 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Sha384);
2020-08-22 14:20:59 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Category);
});
2020-08-22 14:20:59 +01:00
2024-11-09 01:37:59 +00:00
modelBuilder.Entity<Machine>(entity =>
{
entity.HasIndex(e => e.Name);
2024-11-09 01:37:59 +00:00
entity.HasOne(e => e.RomSet).WithMany(e => e.Machines).OnDelete(DeleteBehavior.Cascade);
});
2024-11-09 01:37:59 +00:00
modelBuilder.Entity<FileByMachine>(entity =>
{
entity.HasIndex(e => e.Name);
2024-11-09 01:37:59 +00:00
entity.HasOne(e => e.Machine).WithMany(e => e.Files).OnDelete(DeleteBehavior.Cascade);
2020-09-04 02:12:38 +01:00
2024-11-09 01:37:59 +00:00
entity.HasOne(e => e.File).WithMany(e => e.Machines).OnDelete(DeleteBehavior.Cascade);
});
2020-09-04 02:12:38 +01:00
2024-11-09 01:37:59 +00:00
modelBuilder.Entity<DbDisk>(entity =>
{
entity.HasIndex(e => e.Md5);
2020-09-04 02:12:38 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Sha1);
2020-09-04 22:27:05 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Size);
2020-09-04 02:12:38 +01:00
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.IsInRepo);
});
modelBuilder.Entity<DiskByMachine>(entity =>
{
entity.HasIndex(e => e.Name);
2020-09-04 02:12:38 +01:00
2024-11-09 01:37:59 +00:00
entity.HasOne(e => e.Machine).WithMany(e => e.Disks).OnDelete(DeleteBehavior.Cascade);
2020-09-04 02:12:38 +01:00
2024-11-09 01:37:59 +00:00
entity.HasOne(e => e.Disk).WithMany(e => e.Machines).OnDelete(DeleteBehavior.Cascade);
});
2024-11-09 01:37:59 +00:00
modelBuilder.Entity<DbMedia>(entity =>
{
entity.HasIndex(e => e.Md5);
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Sha1);
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Sha256);
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.SpamSum);
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.Size);
2024-11-09 01:37:59 +00:00
entity.HasIndex(e => e.IsInRepo);
});
2024-11-09 01:37:59 +00:00
modelBuilder.Entity<MediaByMachine>(entity =>
{
entity.HasIndex(e => e.Name);
2024-11-09 01:37:59 +00:00
entity.HasOne(e => e.Machine).WithMany(e => e.Medias).OnDelete(DeleteBehavior.Cascade);
2024-11-09 01:37:59 +00:00
entity.HasOne(e => e.Media).WithMany(e => e.Machines).OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<RomSetStat>(entity => entity.HasOne(e => e.RomSet).WithOne(e => e.Statistics));
2020-08-21 23:21:01 +01:00
}
}