63 lines
2.9 KiB
C#
63 lines
2.9 KiB
C#
using ChaosBot.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace ChaosBot
|
|
{
|
|
public class DatabaseContext : DbContext
|
|
{
|
|
public DbSet<LodestoneCharacter> LodestoneCharacter { get; set; }
|
|
public DbSet<LodestoneFreeCompany> LodestoneFreeCompany { get; set; }
|
|
public DbSet<Point> Points { get; set; }
|
|
public DbSet<Raffle> Raffles { get; set; }
|
|
public DbSet<CommandPermission> CommandPermissions { get; set; }
|
|
public DbSet<Configuration> Configuration { get; set; }
|
|
public DbSet<Experience> ExperiencePoints { get; set; }
|
|
public DbSet<RoleReaction> RoleReactions { get; set; }
|
|
public DbSet<CustomCommand> CustomCommands { get; set; }
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
IConfiguration appSettingsHandler = Program.AppSettingsHandler;
|
|
|
|
if (Program.AppSettingsHandler == null)
|
|
{
|
|
appSettingsHandler = new ConfigurationBuilder()
|
|
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
|
|
.AddJsonFile(Program.AppSettingsPath).Build();
|
|
}
|
|
|
|
string server = appSettingsHandler.GetValue<string>("Database:Host");
|
|
int port = appSettingsHandler.GetValue<int>("Database:Port");
|
|
string user = appSettingsHandler.GetValue<string>("Database:User");
|
|
string pass = appSettingsHandler.GetValue<string>("Database:Pass");
|
|
string name = appSettingsHandler.GetValue<string>("Database:Name");
|
|
|
|
optionsBuilder.UseMySql(
|
|
$"server={server};port={port};user={user};password={pass};database={name}",
|
|
x => x.ServerVersion("5.5.64-mariadb"));
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<LodestoneCharacter>()
|
|
.HasKey(x => new {x.DiscordGuildId, x.LodestoneId});
|
|
modelBuilder.Entity<LodestoneFreeCompany>()
|
|
.HasKey(x => new {x.DiscordGuildId, x.LodestoneId});
|
|
modelBuilder.Entity<Point>()
|
|
.HasKey(x => new {x.DiscordGuildId, x.DiscordUserId});
|
|
modelBuilder.Entity<Experience>()
|
|
.HasKey(x => new {x.DiscordGuildId, x.DiscordUserId});
|
|
modelBuilder.Entity<Configuration>()
|
|
.HasKey(x => new {x.DiscordGuildId, x.Key});
|
|
modelBuilder.Entity<RoleReaction>()
|
|
.HasKey(x => new {x.DiscordGuildId, x.DiscordMessageId, x.DiscordRoleId, x.DiscordEmoteNameEncoded});
|
|
modelBuilder.Entity<CustomCommand>()
|
|
.HasKey(x => new {x.DiscordGuildId, x.Command});
|
|
}
|
|
}
|
|
}
|