57 lines
2.8 KiB
C#
57 lines
2.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace ChaosBot.Models
|
|
{
|
|
public class ChaosbotContext : 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)
|
|
{
|
|
if (Program.AppSettingsHandler == null)
|
|
{
|
|
Program.AppSettingsHandler = new ConfigurationBuilder()
|
|
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
|
|
.AddJsonFile("./appsettings.json", optional: false, reloadOnChange: true).Build();
|
|
}
|
|
string server = Program.AppSettingsHandler.GetValue<string>("Database:Host");
|
|
int port = Program.AppSettingsHandler.GetValue<int>("Database:Port");
|
|
string user = Program.AppSettingsHandler.GetValue<string>("Database:User");
|
|
string pass = Program.AppSettingsHandler.GetValue<string>("Database:Pass");
|
|
string name = Program.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});
|
|
}
|
|
}
|
|
} |