/// <summary> /// Initalizes Database on Server Side, resets database if already exists /// </summary> /// <returns></returns> public static async Task CreateDatabaseOnServerAsync(string username, string password) { /*if (File.Exists(Settings.Settings.DbFilename)) { File.Delete(Settings.Settings.DbFilename); }*/ LogManager.log("reinitalizing new database", LogCategory.info, LogTopic.Database); // Create Pangolin context for mysql DbContextOptionsBuilder<PangolinContext> optionsBuilder = new DbContextOptionsBuilder<PangolinContext>(); string mysqlConString = "Server=" + Settings.Settings.DatabaseHostAddress + ";Port=" + Settings.Settings.DatabasePort + ";Database=database;Uid=" + username + ";Pwd=" + password + ";"; LogManager.logInfo("Initalizing database with connection string: '" + mysqlConString + "'", Logger.LogTopic.Database); optionsBuilder.UseMySql(mysqlConString); PangolinContext pr = new PangolinContext(optionsBuilder.Options); // delete old database and create new one LogManager.logInfo("Deleting old mysql database", Logger.LogTopic.Database); await pr.Database.EnsureDeletedAsync(); LogManager.logInfo("Creating new database on mysql server", Logger.LogTopic.Database); await pr.Database.EnsureCreatedAsync(); LogManager.logInfo("Adding default admin user on mysql", Logger.LogTopic.User); await UserManagment.AddDefaultAdminUser(mysqlConString, pr); LogManager.logInfo("Disposing database", Logger.LogTopic.User); pr.Dispose(); }
/// <summary> /// Singelton-Pattern to get database /// </summary> /// <returns></returns> public static PangolinContext GetDatabase() { if (database == null) { LogManager.log("Database is null, creating database context", LogCategory.info, LogTopic.Database); database = new PangolinContext(); LogManager.log("Ensuring database was created", LogCategory.info, LogTopic.Database); database.Database.EnsureCreated(); } return(database); }