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 options) : base(options) { } public TaikoDbContext(string dbFilePath) { this.dbFilePath = dbFilePath; } public virtual DbSet Cards { get; set; } = null!; public virtual DbSet SongBestData { get; set; } = null!; public virtual DbSet SongPlayData { get; set; } = null!; public virtual DbSet 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(entity => { entity.HasKey(e => e.AccessCode); entity.ToTable("Card"); entity.HasIndex(e => e.Baid, "IX_Card_Baid") .IsUnique(); }); modelBuilder.Entity(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(); entity.Property(e => e.BestCrown) .HasConversion(); entity.Property(e => e.BestScoreRank) .HasConversion(); }); modelBuilder.Entity(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(); entity.Property(e => e.ScoreRank) .HasConversion(); entity.Property(e => e.Crown) .HasConversion(); }); modelBuilder.Entity(entity => { entity.HasKey(e => e.Baid); entity.Property(e => e.LastPlayDatetime).HasColumnType("datetime"); entity.HasOne(d => d.Ba) .WithMany() .HasPrincipalKey(p => p.Baid) .HasForeignKey(d => d.Baid) .OnDelete(DeleteBehavior.Cascade); entity.Property(e => e.AchievementDisplayDifficulty) .HasConversion(); }); OnModelCreatingPartial(modelBuilder); } partial void OnModelCreatingPartial(ModelBuilder modelBuilder); } }