1
0
mirror of synced 2024-12-01 01:27:21 +01:00
TaikoLocalServer/GameDatabase/Context/TaikoDbContext.cs

133 lines
4.1 KiB
C#
Raw Normal View History

using GameDatabase.Entities;
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!;
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
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}");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Card>(entity =>
{
entity.HasKey(e => e.AccessCode);
entity.ToTable("Card");
});
modelBuilder.Entity<Credential>(entity =>
{
entity.HasKey(e => e.Baid);
2022-08-25 09:36:23 +02: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.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.AchievementDisplayDifficulty)
.HasConversion<uint>();
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
2022-10-26 18:42:41 +02:00
}