1
0
mirror of synced 2024-11-24 15:00:16 +01:00
TaikoLocalServer/GameDatabase/Context/TaikoDbContext.cs
2023-11-11 23:12:26 +00:00

132 lines
4.1 KiB
C#

using GameDatabase.Entities;
using Microsoft.EntityFrameworkCore;
using SharedProject.Utils;
namespace GameDatabase.Context
{
public partial class TaikoDbContext : DbContext
{
private string? dbFilePath;
public TaikoDbContext()
{
}
public TaikoDbContext(DbContextOptions<TaikoDbContext> options)
: base(options)
{
}
public TaikoDbContext(string dbFilePath)
{
this.dbFilePath = dbFilePath;
}
public virtual DbSet<Card> Cards { get; set; } = null!;
public virtual DbSet<Credential> Credentials { get; set; } = null!;
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;
}
var path = Path.Combine(PathHelper.GetRootPath(), "taiko.db3");
if (dbFilePath is not null)
{
path = dbFilePath;
}
optionsBuilder.UseSqlite($"Data Source={path}");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Card>(entity =>
{
entity.HasKey(e => e.AccessCode);
entity.ToTable("Card");
entity.HasOne(d => d.Ba)
.WithMany()
.HasPrincipalKey(p => p.Baid)
.HasForeignKey(d => d.Baid)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<Credential>(entity =>
{
entity.HasKey(e => e.Baid);
entity.ToTable("Credential");
entity.HasOne(d => d.Ba)
.WithMany()
.HasPrincipalKey(p => p.Baid)
.HasForeignKey(d => d.Baid)
.OnDelete(DeleteBehavior.Cascade);
});
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>();
entity.Property(e => e.BestCrown)
.HasConversion<uint>();
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);
entity.Property(e => e.Difficulty)
.HasConversion<uint>();
entity.Property(e => e.ScoreRank)
.HasConversion<uint>();
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);
}
}