Refactor DatabaseContext to be cleaner cause EF is annoying like this.
This commit is contained in:
parent
ae1a15ad32
commit
67bde5a610
@ -20,31 +20,20 @@ namespace ChaosBot
|
|||||||
{
|
{
|
||||||
if (!optionsBuilder.IsConfigured)
|
if (!optionsBuilder.IsConfigured)
|
||||||
{
|
{
|
||||||
string server, user, pass, name;
|
IConfiguration appSettingsHandler = Program.AppSettingsHandler;
|
||||||
int port;
|
|
||||||
|
|
||||||
if (Program.AppSettingsHandler == null)
|
if (Program.AppSettingsHandler == null)
|
||||||
{
|
{
|
||||||
IConfiguration config = Program.AppSettingsHandler = new ConfigurationBuilder()
|
appSettingsHandler = new ConfigurationBuilder()
|
||||||
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
|
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
|
||||||
.AddJsonFile("./appsettings.json", optional: false, reloadOnChange: true).Build();
|
.AddJsonFile(Program.AppSettingsPath).Build();
|
||||||
|
|
||||||
server = config.GetValue<string>("Database:Host");
|
|
||||||
port = config.GetValue<int>("Database:Port");
|
|
||||||
user = config.GetValue<string>("Database:User");
|
|
||||||
pass = config.GetValue<string>("Database:Pass");
|
|
||||||
name = config.GetValue<string>("Database:Name");
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
ConfigHelpers.Configuration config = new ConfigHelpers.Configuration();
|
|
||||||
|
|
||||||
server = config.GetByKey<string>("Database:Host").GetValue(readRestricted: true);
|
string server = appSettingsHandler.GetValue<string>("Database:Host");
|
||||||
port = config.GetByKey<int>("Database:Port").GetValue(readRestricted: true);
|
int port = appSettingsHandler.GetValue<int>("Database:Port");
|
||||||
user = config.GetByKey<string>("Database:User").GetValue(readRestricted: true);
|
string user = appSettingsHandler.GetValue<string>("Database:User");
|
||||||
pass = config.GetByKey<string>("Database:Pass").GetValue(readRestricted: true);
|
string pass = appSettingsHandler.GetValue<string>("Database:Pass");
|
||||||
name = config.GetByKey<string>("Database:Name").GetValue(readRestricted: true);
|
string name = appSettingsHandler.GetValue<string>("Database:Name");
|
||||||
}
|
|
||||||
|
|
||||||
optionsBuilder.UseMySql(
|
optionsBuilder.UseMySql(
|
||||||
$"server={server};port={port};user={user};password={pass};database={name}",
|
$"server={server};port={port};user={user};password={pass};database={name}",
|
||||||
|
|||||||
@ -15,18 +15,16 @@ namespace ChaosBot
|
|||||||
private static ILogger _logger;
|
private static ILogger _logger;
|
||||||
public static IConfiguration AppSettingsHandler;
|
public static IConfiguration AppSettingsHandler;
|
||||||
|
|
||||||
private static string _appSettingsPath;
|
public const string AppSettingsPath = "./appsettings.json";
|
||||||
|
|
||||||
private static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
_appSettingsPath = args.Length > 0 ? args[0] : "./appsettings.json";
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Load configuration from AppSettings.Json and save as Cfg
|
* Load configuration from AppSettings.Json and save as Cfg
|
||||||
*/
|
*/
|
||||||
AppSettingsHandler = LoadConfiguration(_appSettingsPath);
|
AppSettingsHandler = LoadConfiguration(AppSettingsPath);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize the _logger for logging purposes
|
* Initialize the _logger for logging purposes
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user