2024-04-18 07:12:10 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-05-01 09:11:11 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
|
|
|
using Newtonsoft.Json;
|
2024-04-18 07:12:10 +00:00
|
|
|
|
using SCHALE.Common.Database.Models;
|
|
|
|
|
|
|
|
|
|
namespace SCHALE.Common.Database
|
|
|
|
|
{
|
|
|
|
|
public class SCHALEContext : DbContext
|
|
|
|
|
{
|
2024-04-25 03:50:09 +00:00
|
|
|
|
public DbSet<GuestAccount> GuestAccounts { get; set; }
|
2024-04-28 01:41:38 +00:00
|
|
|
|
public DbSet<AccountDB> Accounts { get; set; }
|
|
|
|
|
public DbSet<MissionProgressDB> MissionProgresses { get; set; }
|
2024-06-02 02:16:53 +00:00
|
|
|
|
|
2024-05-01 09:11:11 +00:00
|
|
|
|
public DbSet<ItemDB> Items { get; set; }
|
|
|
|
|
public DbSet<CharacterDB> Characters { get; set; }
|
2024-05-11 04:30:17 +00:00
|
|
|
|
public DbSet<EquipmentDB> Equipment { get; set; }
|
|
|
|
|
public DbSet<WeaponDB> Weapons { get; set; }
|
2024-05-18 22:00:19 +00:00
|
|
|
|
public DbSet<GearDB> Gears { get; set; }
|
2024-06-02 02:16:53 +00:00
|
|
|
|
|
2024-05-30 04:46:17 +00:00
|
|
|
|
public DbSet<MemoryLobbyDB> MemoryLobbies { get; set; }
|
|
|
|
|
public DbSet<ScenarioHistoryDB> Scenarios { get; set; }
|
2024-05-11 04:30:17 +00:00
|
|
|
|
|
2024-05-01 09:11:11 +00:00
|
|
|
|
public DbSet<EchelonDB> Echelons { get; set; }
|
|
|
|
|
public DbSet<AccountTutorial> AccountTutorials { get; set; }
|
2024-06-02 02:16:53 +00:00
|
|
|
|
|
2024-11-09 09:11:59 +00:00
|
|
|
|
public DbSet<CafeDB> Cafes { get; set; }
|
|
|
|
|
public DbSet<FurnitureDB> Furnitures { get; set; }
|
|
|
|
|
|
2024-12-29 16:52:25 +00:00
|
|
|
|
public DbSet<AccountCurrencyDB> Currencies { get; set; }
|
|
|
|
|
public DbSet<MultiFloorRaidDB> MultiFloorRaids { get; set; }
|
|
|
|
|
public DbSet<WeekDungeonStageHistoryDB> WeekDungeonStageHistories { get; set; }
|
|
|
|
|
public DbSet<SchoolDungeonStageHistoryDB> SchoolDungeonStageHistories { get; set; }
|
|
|
|
|
public DbSet<CampaignStageHistoryDB> CampaignStageHistories { get; set; }
|
|
|
|
|
public DbSet<MailDB> Mails { get; set; }
|
|
|
|
|
|
2024-05-01 09:11:11 +00:00
|
|
|
|
public static SCHALEContext Create(string connectionString) =>
|
|
|
|
|
new(new DbContextOptionsBuilder<SCHALEContext>()
|
|
|
|
|
.UseSqlServer(connectionString)
|
|
|
|
|
.Options);
|
2024-04-18 07:12:10 +00:00
|
|
|
|
|
2024-06-02 02:16:53 +00:00
|
|
|
|
public SCHALEContext()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SCHALEContext(DbContextOptions options) : base(options)
|
2024-04-18 07:12:10 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
base.OnModelCreating(modelBuilder);
|
2024-04-25 03:50:09 +00:00
|
|
|
|
|
2024-05-01 09:11:11 +00:00
|
|
|
|
modelBuilder.Entity<GuestAccount>().Property(x => x.Uid).ValueGeneratedOnAdd();
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<AccountDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
modelBuilder.Entity<AccountDB>()
|
|
|
|
|
.HasMany(x => x.Items)
|
|
|
|
|
.WithOne(x => x.Account)
|
|
|
|
|
.HasForeignKey(x => x.AccountServerId)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
modelBuilder.Entity<AccountDB>()
|
|
|
|
|
.HasMany(x => x.Characters)
|
|
|
|
|
.WithOne(x => x.Account)
|
|
|
|
|
.HasForeignKey(x => x.AccountServerId)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
modelBuilder.Entity<AccountDB>()
|
|
|
|
|
.HasMany(x => x.MissionProgresses)
|
|
|
|
|
.WithOne(x => x.Account)
|
|
|
|
|
.HasForeignKey(x => x.AccountServerId)
|
|
|
|
|
.IsRequired();
|
2024-05-11 04:30:17 +00:00
|
|
|
|
modelBuilder.Entity<AccountDB>()
|
|
|
|
|
.HasMany(x => x.Equipment)
|
|
|
|
|
.WithOne(x => x.Account)
|
|
|
|
|
.HasForeignKey(x => x.AccountServerId)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
modelBuilder.Entity<AccountDB>()
|
|
|
|
|
.HasMany(x => x.Weapons)
|
|
|
|
|
.WithOne(x => x.Account)
|
|
|
|
|
.HasForeignKey(x => x.AccountServerId)
|
|
|
|
|
.IsRequired();
|
2024-05-18 22:00:19 +00:00
|
|
|
|
modelBuilder.Entity<AccountDB>()
|
|
|
|
|
.HasMany(x => x.Gears)
|
|
|
|
|
.WithOne(x => x.Account)
|
|
|
|
|
.HasForeignKey(x => x.AccountServerId)
|
|
|
|
|
.IsRequired();
|
2024-05-30 04:46:17 +00:00
|
|
|
|
modelBuilder.Entity<AccountDB>()
|
|
|
|
|
.HasMany(x => x.MemoryLobbies)
|
|
|
|
|
.WithOne(x => x.Account)
|
|
|
|
|
.HasForeignKey(x => x.AccountServerId)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
modelBuilder.Entity<AccountDB>()
|
|
|
|
|
.HasMany(x => x.Scenarios)
|
|
|
|
|
.WithOne(x => x.Account)
|
|
|
|
|
.HasForeignKey(x => x.AccountServerId)
|
|
|
|
|
.IsRequired();
|
2024-11-09 09:11:59 +00:00
|
|
|
|
modelBuilder.Entity<AccountDB>()
|
|
|
|
|
.HasMany(x => x.Cafes)
|
|
|
|
|
.WithOne(x => x.Account)
|
|
|
|
|
.HasForeignKey(x => x.AccountServerId)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
modelBuilder.Entity<AccountDB>()
|
|
|
|
|
.HasMany(x => x.Furnitures)
|
|
|
|
|
.WithOne(x => x.Account)
|
|
|
|
|
.HasForeignKey(x => x.AccountServerId)
|
|
|
|
|
.IsRequired();
|
2024-05-30 04:46:17 +00:00
|
|
|
|
|
2024-05-21 14:25:30 +00:00
|
|
|
|
modelBuilder.Entity<AccountDB>(x => x.Property(b => b.RaidInfo).HasJsonConversion());
|
2024-05-01 09:11:11 +00:00
|
|
|
|
modelBuilder.Entity<ItemDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
2024-05-11 04:30:17 +00:00
|
|
|
|
modelBuilder.Entity<EquipmentDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
modelBuilder.Entity<WeaponDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
2024-05-18 22:00:19 +00:00
|
|
|
|
modelBuilder.Entity<GearDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
2024-05-11 04:30:17 +00:00
|
|
|
|
|
2024-05-01 09:11:11 +00:00
|
|
|
|
modelBuilder.Entity<EchelonDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
|
2024-11-09 09:11:59 +00:00
|
|
|
|
modelBuilder.Entity<CafeDB>().Property(x => x.CafeDBId).ValueGeneratedOnAdd();
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<CafeDB>().Property(x => x.CafeVisitCharacterDBs).HasJsonConversion();
|
|
|
|
|
modelBuilder.Entity<CafeDB>().Property(x => x.CurrencyDict_Obsolete).HasJsonConversion();
|
|
|
|
|
modelBuilder.Entity<CafeDB>().Property(x => x.UpdateTimeDict_Obsolete).HasJsonConversion();
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<FurnitureDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
|
2024-05-01 09:11:11 +00:00
|
|
|
|
modelBuilder.Entity<CharacterDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
modelBuilder.Entity<CharacterDB>().Property(x => x.EquipmentSlotAndDBIds).HasJsonConversion();
|
|
|
|
|
modelBuilder.Entity<CharacterDB>().Property(x => x.PotentialStats).HasJsonConversion();
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<AccountTutorial>().Property(x => x.AccountServerId).ValueGeneratedNever();
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<MissionProgressDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
modelBuilder.Entity<MissionProgressDB>().Property(x => x.ProgressParameters).HasJsonConversion();
|
2024-12-29 16:52:25 +00:00
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<AccountCurrencyDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
modelBuilder.Entity<AccountCurrencyDB>().Property(x => x.CurrencyDict).HasJsonConversion();
|
|
|
|
|
modelBuilder.Entity<AccountCurrencyDB>().Property(x => x.UpdateTimeDict).HasJsonConversion();
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<MultiFloorRaidDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
modelBuilder.Entity<MultiFloorRaidDB>().Property(x => x.TotalReceivableRewards).HasJsonConversion();
|
|
|
|
|
modelBuilder.Entity<MultiFloorRaidDB>().Property(x => x.TotalReceivedRewards).HasJsonConversion();
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<WeekDungeonStageHistoryDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
modelBuilder.Entity<WeekDungeonStageHistoryDB>().Property(x => x.StarGoalRecord).HasJsonConversion();
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<SchoolDungeonStageHistoryDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
modelBuilder.Entity<ScenarioGroupHistoryDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
modelBuilder.Entity<CampaignStageHistoryDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
|
|
|
|
modelBuilder.Entity<MailDB>().Property(x => x.ServerId).ValueGeneratedOnAdd();
|
2024-04-28 01:41:38 +00:00
|
|
|
|
}
|
2024-05-01 09:11:11 +00:00
|
|
|
|
}
|
2024-04-28 01:41:38 +00:00
|
|
|
|
|
2024-06-02 02:16:53 +00:00
|
|
|
|
public class SCHALESqliteContext : SCHALEContext
|
|
|
|
|
{
|
|
|
|
|
public SCHALESqliteContext() { }
|
|
|
|
|
|
|
|
|
|
public SCHALESqliteContext(DbContextOptions options) : base(options)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static SCHALESqliteContext Create(string connectionString) =>
|
|
|
|
|
new(new DbContextOptionsBuilder<SCHALESqliteContext>()
|
|
|
|
|
.UseSqlite(connectionString).Options);
|
|
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
|
{
|
|
|
|
|
optionsBuilder.UseSqlite("Data Source=schale.sqlite3");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 09:11:11 +00:00
|
|
|
|
public static class PropertyBuilderExtensions
|
|
|
|
|
{
|
|
|
|
|
public static PropertyBuilder<T> HasJsonConversion<T>(this PropertyBuilder<T> propertyBuilder) where T : class, new()
|
2024-04-18 07:12:10 +00:00
|
|
|
|
{
|
2024-05-01 09:11:11 +00:00
|
|
|
|
ValueConverter<T, string> converter = new
|
|
|
|
|
(
|
|
|
|
|
v => JsonConvert.SerializeObject(v),
|
|
|
|
|
v => JsonConvert.DeserializeObject<T>(v) ?? new T()
|
|
|
|
|
);
|
2024-04-25 03:50:09 +00:00
|
|
|
|
|
2024-05-01 09:11:11 +00:00
|
|
|
|
propertyBuilder.HasConversion(converter);
|
|
|
|
|
propertyBuilder.Metadata.SetValueConverter(converter);
|
2024-04-25 03:50:09 +00:00
|
|
|
|
|
2024-05-01 09:11:11 +00:00
|
|
|
|
return propertyBuilder;
|
2024-04-25 03:50:09 +00:00
|
|
|
|
}
|
2024-04-18 07:12:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|