public async Task <OperationResult> UpdateConnectionString(InitialSettingsModel initialSettingsModel) { var customerNo = initialSettingsModel.GeneralAppSetupSettingsModel.CustomerNo.ToString(); var dbNamePrefix = ""; if (initialSettingsModel.ConnectionStringSdk.PrefixAllDatabases) { dbNamePrefix = "Microting_"; } var sdkDbName = $"{dbNamePrefix}{customerNo}_SDK"; var angularDbName = $"{dbNamePrefix}{customerNo}_Angular"; var sdkConnectionString = $"host= {initialSettingsModel.ConnectionStringSdk.Host};" + $"Database={sdkDbName};{initialSettingsModel.ConnectionStringSdk.Auth}" + $"port={initialSettingsModel.ConnectionStringSdk.Port};" + "Convert Zero Datetime = true;SslMode=none;"; var angularConnectionString = $"host= {initialSettingsModel.ConnectionStringSdk.Host};" + $"Database={angularDbName};{initialSettingsModel.ConnectionStringSdk.Auth}" + $"port={initialSettingsModel.ConnectionStringSdk.Port};" + "Convert Zero Datetime = true;SslMode=none;"; if (!string.IsNullOrEmpty(_connectionStringsSdk.Value.SdkConnection)) { return(new OperationResult(false, _localizationService.GetString("ConnectionStringAlreadyExist"))); } try { Log.LogEvent($"SettingsService.ConnectionStringExist: connection string is {sdkConnectionString}"); var adminTools = new AdminTools(sdkConnectionString); // Setup SDK DB await adminTools.DbSetup(initialSettingsModel.ConnectionStringSdk.Token); // var core = await _coreHelper.GetCore(); Core core = new Core(); await core.StartSqlOnly(sdkConnectionString); await core.SetSdkSetting(Settings.customerNo, customerNo); } catch (Exception exception) { _logger.LogError(exception.Message); _logger.LogError(exception.StackTrace); if (exception.InnerException != null) { return(new OperationResult(false, exception.Message + " - " + exception.InnerException.Message)); } return(new OperationResult(false, exception.Message)); //return new OperationResult(false, // _localizationService.GetString("SDKConnectionStringIsInvalid")); } // Migrate DB var dbContextOptionsBuilder = new DbContextOptionsBuilder <BaseDbContext>(); try { dbContextOptionsBuilder.UseMySql(angularConnectionString, new MariaDbServerVersion( new Version(10, 4, 0)), b => b.EnableRetryOnFailure()); await using var dbContext = new BaseDbContext(dbContextOptionsBuilder.Options); await dbContext.Database.MigrateAsync(); if (initialSettingsModel.AdminSetupModel != null) { // Seed admin and demo users await SeedAdminHelper.SeedAdmin(initialSettingsModel.AdminSetupModel, initialSettingsModel.GeneralAppSetupSettingsModel.DefaultLocale, dbContext); } } catch (Exception exception) { _logger.LogError(exception.Message); _logger.LogError(exception.StackTrace); //return new OperationResult(false, // _localizationService.GetString("MainConnectionStringIsInvalid")); if (exception.Message == "Could not create the user") { return(new OperationResult(false, _localizationService.GetString(exception.Message))); } if (exception.InnerException != null) { return(new OperationResult(false, exception.Message + " - " + exception.InnerException.Message)); } return(new OperationResult(false, exception.Message)); } try { // Generate SigningKey var key = new byte[32]; RandomNumberGenerator.Create().GetBytes(key); var signingKey = Convert.ToBase64String(key); // Update Database settings await using var dbContext = new BaseDbContext(dbContextOptionsBuilder.Options); await _tokenOptions.UpdateDb((options) => { options.SigningKey = signingKey; }, dbContext); await _applicationSettings.UpdateDb( options => { options.DefaultLocale = initialSettingsModel.GeneralAppSetupSettingsModel.DefaultLocale; }, dbContext); await _connectionStringsSdk.UpdateDb((options) => { options.SdkConnection = sdkConnectionString; }, dbContext); // Update connection string _connectionStrings.UpdateFile((options) => { options.DefaultConnection = angularConnectionString; }); } catch (Exception exception) { _logger.LogError(exception.Message); _logger.LogError(exception.StackTrace); return(exception.InnerException != null ? new OperationResult(false, exception.Message + " - " + exception.InnerException.Message) : new OperationResult(false, exception.Message)); //return new OperationResult(false, // _localizationService.GetString("CouldNotWriteConnectionString")); } Program.Restart(); return(new OperationResult(true)); }