Beispiel #1
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            AutofacConfig.ConfigureContainer();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            // Enable swagger
            //if (Debugger.IsAttached)
            //{
            SwaggerConfig.Register(GlobalConfiguration.Configuration);
            //}

            // Migrations
            try
            {
                var configuration = WebConfigurationManager.OpenWebConfiguration("~");
                var section       = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
                var connString    = section.ConnectionStrings["eFormMainConnection"].ConnectionString;
                if (!connString.IsNullOrEmpty())
                {
                    var migrationConfiguration = new EformMigrationsConfiguration(connString)
                    {
                        TargetDatabase = new DbConnectionInfo(connString, "System.Data.SqlClient")
                    };
                    var migrator = new DbMigrator(migrationConfiguration);
                    migrator.Update();
                }
            }
            catch (Exception exception)
            {
                throw new Exception($"Error while migrate database: {exception.Message}");
            }
        }
        public OperationResult UpdateConnectionString(InitialSettingsModel initialSettingsModel)
        {
            if (!ModelState.IsValid)
            {
                return(new OperationResult(false, "Required fields are not filled"));
            }

            var        inputPath = System.Web.Hosting.HostingEnvironment.MapPath("~/bin/Input.txt");
            AdminTools adminTools;
            var        sdkConnectionString = "Data Source="
                                             + initialSettingsModel.ConnectionStringSdk.Source + ";Initial Catalog="
                                             + initialSettingsModel.ConnectionStringSdk.Catalogue + ";"
                                             + initialSettingsModel.ConnectionStringSdk.Auth;

            var mainConnectionString = "Data Source="
                                       + initialSettingsModel.ConnectionStringMain.Source + ";Initial Catalog="
                                       + initialSettingsModel.ConnectionStringMain.Catalogue + ";"
                                       + initialSettingsModel.ConnectionStringMain.Auth;

            try
            {
                if (File.Exists(inputPath))
                {
                    //File.Delete(inputPath);
                    //var fileStream = File.Create(inputPath);
                    //fileStream.Dispose();
                    return(new OperationResult(false, "Connection string already exist"));
                }

                var fileStream = File.Create(inputPath);
                fileStream.Dispose();

                File.WriteAllText(inputPath, sdkConnectionString);
            }
            catch (Exception exception)
            {
                Logger.Error(exception.Message);
                return(new OperationResult(false, "Could not write connection string in /bin/Input.txt"));
            }
            try
            {
                adminTools = new AdminTools(sdkConnectionString);
            }
            catch (Exception exception)
            {
                Logger.Error(exception.Message);
                return(new OperationResult(false, "SDK connection string is invalid"));
            }

            var configuration = WebConfigurationManager.OpenWebConfiguration("~");
            var section       = (ConnectionStringsSection)configuration.GetSection("connectionStrings");

            section.ConnectionStrings["eFormMainConnection"].ConnectionString = mainConnectionString;
            try
            {
                configuration.Save();
                ConfigurationManager.RefreshSection("connectionStrings");
                var migrationConfiguration = new EformMigrationsConfiguration(mainConnectionString)
                {
                    TargetDatabase = new DbConnectionInfo(mainConnectionString, "System.Data.SqlClient")
                };
                var migrator = new DbMigrator(migrationConfiguration);
                migrator.Update();
                var settingsHelper = new SettingsHelper(mainConnectionString);
                settingsHelper.CreateAdminUser(initialSettingsModel.AdminSetupModel);
            }
            catch (Exception exception)
            {
                Logger.Error(exception.Message);
                return(new OperationResult(false, "Main connection string is invalid"));
            }

            adminTools.DbSetup(initialSettingsModel.ConnectionStringSdk.Token);
            return(new OperationResult(true));
        }
Beispiel #3
0
        public OperationResult UpdateConnectionString(InitialSettingsModel initialSettingsModel)
        {
            if (!ModelState.IsValid)
            {
                return(new OperationResult(false, LocaleHelper.GetString("RequestFieldsAreNotFilled")));
            }
            var sdkConnectionString = initialSettingsModel.ConnectionStringSdk.Source + ";Initial Catalog="
                                      + initialSettingsModel.ConnectionStringSdk.Catalogue + ";"
                                      + initialSettingsModel.ConnectionStringSdk.Auth;

            var mainConnectionString = initialSettingsModel.ConnectionStringMain.Source + ";Initial Catalog="
                                       + initialSettingsModel.ConnectionStringMain.Catalogue + ";"
                                       + initialSettingsModel.ConnectionStringMain.Auth;
            // Save SDK connection string
            var inputPath = System.Web.Hosting.HostingEnvironment.MapPath("~/bin/Input.txt");

            if (inputPath == null)
            {
                return(new OperationResult(false, LocaleHelper.GetString("ErrorWhileCreatingFileForSDK")));
            }
            AdminTools adminTools;

            try
            {
                if (File.Exists(inputPath))
                {
                    return(new OperationResult(false, LocaleHelper.GetString("ConnectionStringAlreadyExist")));
                }

                var fileStream = File.Create(inputPath);
                fileStream.Dispose();

                File.WriteAllText(inputPath, sdkConnectionString);
            }
            catch (Exception exception)
            {
                Logger.Error(exception.Message);
                return(new OperationResult(false, LocaleHelper.GetString("CouldNotWriteConnectionString")));
            }
            try
            {
                adminTools = new AdminTools(sdkConnectionString);
            }
            catch (Exception exception)
            {
                Logger.Error(exception.Message);
                return(new OperationResult(false, LocaleHelper.GetString("SDKConnectionStringIsInvalid")));
            }
            // Save Main connection string
            var configuration      = WebConfigurationManager.OpenWebConfiguration("~");
            var connStringsSection = (ConnectionStringsSection)configuration.GetSection("connectionStrings");

            connStringsSection.ConnectionStrings["eFormMainConnection"].ConnectionString = mainConnectionString;
            // Save general app settings
            var section = (AppSettingsSection)configuration.GetSection("appSettings");

            section.Settings["general:defaultLocale"].Value =
                initialSettingsModel.GeneralAppSetupSettingsModel.DefaultLocale;
            // Save settings and migrate DB
            try
            {
                configuration.Save();
                ConfigurationManager.RefreshSection("connectionStrings");
                var migrationConfiguration = new EformMigrationsConfiguration(mainConnectionString)
                {
                    TargetDatabase = new DbConnectionInfo(mainConnectionString, "System.Data.SqlClient")
                };
                var migrator = new DbMigrator(migrationConfiguration);
                migrator.Update();
                var settingsHelper = new SettingsHelper(mainConnectionString);
                settingsHelper.CreateAdminUser(initialSettingsModel.AdminSetupModel);
            }
            catch (Exception exception)
            {
                Logger.Error(exception.Message);
                return(new OperationResult(false, LocaleHelper.GetString("MainConnectionStringIsInvalid")));
            }
            // Setup SDK DB
            adminTools.DbSetup(initialSettingsModel.ConnectionStringSdk.Token);
            return(new OperationResult(true));
        }