2024-03-17 08:17:34 +01:00
|
|
|
|
using EntityFramework.Exceptions.Sqlite;
|
|
|
|
|
using GameDatabase.Entities;
|
2022-10-26 18:42:11 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using SharedProject.Utils;
|
|
|
|
|
|
|
|
|
|
namespace GameDatabase.Context
|
2022-08-25 09:36:23 +02:00
|
|
|
|
{
|
|
|
|
|
public partial class TaikoDbContext : DbContext
|
|
|
|
|
{
|
2022-10-30 17:44:47 +01:00
|
|
|
|
private string? dbFilePath;
|
2022-08-25 09:36:23 +02:00
|
|
|
|
public TaikoDbContext()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TaikoDbContext(DbContextOptions<TaikoDbContext> options)
|
|
|
|
|
: base(options)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-30 17:44:47 +01:00
|
|
|
|
public TaikoDbContext(string dbFilePath)
|
|
|
|
|
{
|
|
|
|
|
this.dbFilePath = dbFilePath;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 09:36:23 +02:00
|
|
|
|
public virtual DbSet<Card> Cards { get; set; } = null!;
|
2023-11-11 22:04:11 +01:00
|
|
|
|
public virtual DbSet<Credential> Credentials { get; set; } = null!;
|
2022-08-25 09:36:23 +02:00
|
|
|
|
public virtual DbSet<SongBestDatum> SongBestData { get; set; } = null!;
|
|
|
|
|
public virtual DbSet<SongPlayDatum> SongPlayData { get; set; } = null!;
|
|
|
|
|
public virtual DbSet<UserDatum> UserData { get; set; } = null!;
|
|
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
|
{
|
|
|
|
|
if (optionsBuilder.IsConfigured)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-26 18:42:41 +02:00
|
|
|
|
|
2022-10-26 18:42:11 +02:00
|
|
|
|
var path = Path.Combine(PathHelper.GetRootPath(), "taiko.db3");
|
2022-10-30 17:44:47 +01:00
|
|
|
|
if (dbFilePath is not null)
|
|
|
|
|
{
|
|
|
|
|
path = dbFilePath;
|
|
|
|
|
}
|
2022-08-25 09:36:23 +02:00
|
|
|
|
optionsBuilder.UseSqlite($"Data Source={path}");
|
2024-03-17 08:17:34 +01:00
|
|
|
|
optionsBuilder.UseExceptionProcessor().EnableSensitiveDataLogging();
|
2022-08-25 09:36:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
modelBuilder.Entity<Card>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasKey(e => e.AccessCode);
|
|
|
|
|
|
|
|
|
|
entity.ToTable("Card");
|
2023-11-12 00:12:26 +01:00
|
|
|
|
|
|
|
|
|
entity.HasOne(d => d.Ba)
|
|
|
|
|
.WithMany()
|
|
|
|
|
.HasPrincipalKey(p => p.Baid)
|
|
|
|
|
.HasForeignKey(d => d.Baid)
|
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
2023-11-11 22:04:11 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<Credential>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasKey(e => e.Baid);
|
2022-08-25 09:36:23 +02:00
|
|
|
|
|
2023-11-11 22:04:11 +01:00
|
|
|
|
entity.ToTable("Credential");
|
|
|
|
|
|
|
|
|
|
entity.HasOne(d => d.Ba)
|
|
|
|
|
.WithMany()
|
|
|
|
|
.HasPrincipalKey(p => p.Baid)
|
|
|
|
|
.HasForeignKey(d => d.Baid)
|
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
2022-08-25 09:36:23 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<SongBestDatum>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasKey(e => new { e.Baid, e.SongId, e.Difficulty });
|
|
|
|
|
|
|
|
|
|
entity.HasOne(d => d.Ba)
|
|
|
|
|
.WithMany()
|
|
|
|
|
.HasPrincipalKey(p => p.Baid)
|
|
|
|
|
.HasForeignKey(d => d.Baid)
|
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
|
|
entity.Property(e => e.Difficulty)
|
|
|
|
|
.HasConversion<uint>();
|
2022-10-26 18:42:41 +02:00
|
|
|
|
|
2022-08-25 09:36:23 +02:00
|
|
|
|
entity.Property(e => e.BestCrown)
|
|
|
|
|
.HasConversion<uint>();
|
2022-10-26 18:42:41 +02:00
|
|
|
|
|
2022-08-25 09:36:23 +02:00
|
|
|
|
entity.Property(e => e.BestScoreRank)
|
|
|
|
|
.HasConversion<uint>();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<SongPlayDatum>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasKey(e => e.Id);
|
|
|
|
|
|
|
|
|
|
entity.Property(e => e.Id).ValueGeneratedOnAdd();
|
|
|
|
|
|
|
|
|
|
entity.Property(e => e.PlayTime).HasColumnType("datetime");
|
|
|
|
|
|
|
|
|
|
entity.HasOne(d => d.Ba)
|
|
|
|
|
.WithMany()
|
|
|
|
|
.HasPrincipalKey(p => p.Baid)
|
|
|
|
|
.HasForeignKey(d => d.Baid)
|
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
2022-10-26 18:42:41 +02:00
|
|
|
|
|
2022-08-25 09:36:23 +02:00
|
|
|
|
entity.Property(e => e.Difficulty)
|
|
|
|
|
.HasConversion<uint>();
|
2022-10-26 18:42:41 +02:00
|
|
|
|
|
2022-08-25 09:36:23 +02:00
|
|
|
|
entity.Property(e => e.ScoreRank)
|
|
|
|
|
.HasConversion<uint>();
|
2022-10-26 18:42:41 +02:00
|
|
|
|
|
2022-08-25 09:36:23 +02:00
|
|
|
|
entity.Property(e => e.Crown)
|
|
|
|
|
.HasConversion<uint>();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<UserDatum>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasKey(e => e.Baid);
|
|
|
|
|
|
|
|
|
|
entity.Property(e => e.LastPlayDatetime).HasColumnType("datetime");
|
|
|
|
|
|
|
|
|
|
entity.Property(e => e.AchievementDisplayDifficulty)
|
|
|
|
|
.HasConversion<uint>();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
OnModelCreatingPartial(modelBuilder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
|
|
|
|
}
|
2022-10-26 18:42:41 +02:00
|
|
|
|
}
|