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-05-11 04:30:17 +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-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-05-11 04:30:17 +00:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
public SCHALEContext(DbContextOptions<SCHALEContext> options) : base(options)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-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();
|
|
|
|
|
|
|
|
|
|
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-04-28 01:41:38 +00:00
|
|
|
|
}
|
2024-05-01 09:11:11 +00:00
|
|
|
|
}
|
2024-04-28 01:41:38 +00:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|