Beispiel #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //var connection = _configuration["ConnectionStrings:PostgresConnectionString"];//Em appsettings.json
            var connectionString = _configuration.GetConnectionString("PostgresConnectionString");

            services.AddEntityFrameworkNpgsql().AddDbContext <SqlContext>(options => options.UseNpgsql(connectionString));

            if (_environment.IsDevelopment())
            {
                try
                {
                    var evolveConnection = new NpgsqlConnection(connectionString);

                    //var evolve = new Evolve.Evolve("evolve.json", evolveConnection, msg => _logger.LogInformation(msg))
                    var evolve = new Evolve.Evolve(evolveConnection, msg => _logger.LogInformation(msg))
                    {
                        Locations = new List <string> {
                            "DataBase/Migrations", "DataBase/DataSet"
                        },
                        IsEraseDisabled = true //para não apagar os dados toda vez que executar
                    };
                    evolve.Migrate();
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("DataBase migration failed.", ex);
                    //throw;
                }
            }

            services.AddMvc();
            services.AddControllers();
            services.AddApiVersioning();

            //Dependency Injection
            services.AddScoped <IPersonBusiness, PersonBusiness>();
            services.AddScoped <IBookBusiness, BookBusiness>();
            services.AddScoped <IPersonRepository, PersonRepository>();

            //Dependency Injection of GenerecRepository
            services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>));
        }
Beispiel #2
0
        public void ConfigureServices(IServiceCollection services)
        {
            //Busca string de conexão ao banco
            var connectionString = _configuration["MySqlConnection:MySqlConnectionString"];

            // Adicionando serviço ao banco MySql
            services.AddDbContext <MySqlContext>(options => options.UseMySql(connectionString));

            if (_environment.IsDevelopment()) // Base de desenvolvimento
            {
                try
                {
                    // Adicionando Migrations de forma Manual 'Evolve'
                    var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
                    var evolve           = new Evolve.Evolve("evolve.json", evolveConnection, msg => _logger.LogInformation(msg))
                    {
                        Locations = new List <string> {
                            "db/migrations"
                        },
                        IsEraseDisabled = true
                    };

                    evolve.Migrate();
                }
                catch (Exception e)
                {
                    _logger.LogCritical("Database migration failed.", e);
                    throw;
                }
            }

            services.AddMvc();

            services.AddApiVersioning(); // Pacote de versionamento da nossa API

            // Injeções de dependencias
            services.AddScoped <IPersonBusiness, PersonBusinessImpl>();
            services.AddScoped <IBookBusiness, BookBusinessImpl>();

            services.AddScoped <IPersonRepository, PersonRepositoryImpl>();
            services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>)); // Injeção de dependencia para genericos
        }
Beispiel #3
0
        private static void MigrateDatabase()
        {
            try
            {
                var cnx    = new Npgsql.NpgsqlConnection(Configuration.GetConnectionString("postgres_sql"));
                var evolve = new Evolve.Evolve(cnx, msg => Log.Information(msg))
                {
                    Locations         = new[] { "Resources/db" },
                    IsEraseDisabled   = true,
                    MetadataTableName = "schema_version"
                };

                evolve.Migrate();
            }
            catch (Exception ex)
            {
                Log.Error("Database migration failed.", ex);
                throw;
            }
        }
Beispiel #4
0
 private void MigratenDatabase(string connection)
 {
     try
     {
         var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connection);
         var evolve           = new Evolve.Evolve(evolveConnection, msg => Log.Information(msg))
         {
             Locations = new List <string> {
                 "DataBaseMigrations/Migrations", "DataBaseMigrations/Dataset"
             },
             IsEraseDisabled = true,
         };
         evolve.Migrate();
     }
     catch (Exception ex)
     {
         Log.Error("Database Migration Falied! - " + ex.ToString(), ex);
         throw ex;
     }
 }
 private void Migration(string connection)
 {
     try
     {
         var envolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connection);
         var Evolve            = new Evolve.Evolve(envolveConnection, msg => Log.Information(msg))
         {
             Locations = new List <string> {
                 "Migrates/migration", "Migrates/dataset"
             },
             IsEraseDisabled = true
         };
         Evolve.Migrate();
     }
     catch (Exception ex)
     {
         Log.Error("Falha na migration ", ex);
         throw;
     }
 }
        private void CheckDatabaseMigrations()
        {
            try
            {
                var cnx = new NpgsqlConnection(Configuration.GetConnectionString("database"));
                var location = Configuration.GetSection("AppSettings")["Evolve.Location"];
                var evolve = new Evolve.Evolve(cnx)
                {
                    Locations = new[] {location},
                    IsEraseDisabled = true,
                };

                evolve.Migrate();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error applying migrations :: <{ex.Message}>", ex);
                throw;
            }
        }
Beispiel #7
0
 private void InitializeDatabase()
 {
     try
     {
         var evolveConnection = new MySqlConnection(Configuration["MySqlConnection:MysqlConnectionString"]);
         var evolve           = new Evolve.Evolve("evolve.json", evolveConnection, msg => _logger.LogInformation(msg))
         {
             Locations = new List <string> {
                 "db/migrations"
             },
             IsEraseDisabled = true
         };
         evolve.Migrate();
     }
     catch (Exception ex)
     {
         _logger.LogCritical("Database migration failed.", ex);
         throw;
     }
 }
        private void InitializeAndConfigureEvolveMigrations()
        {
            try
            {
                var cnx = new NpgsqlConnection(Configuration.GetConnectionString("DefaultConnection"));

                var evolve = new Evolve.Evolve(cnx, msg => _logger.LogInformation(msg))
                {
                    Locations       = new[] { "Resources/db/migration/postgres" },
                    IsEraseDisabled = true,
                };

                evolve.Migrate();
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Database migration failed.", ex);
                throw;
            }
        }
Beispiel #9
0
 private void MigrateDatabase(string connection)
 {
     try
     {
         var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connection);
         var evolve           = new Evolve.Evolve(evolveConnection, msg => Log.Information(msg))
         {
             Locations = new List <string> {
                 "db/migrations", "db/dataset"
             },
             IsEraseDisabled = true
         };
         evolve.Migrate();
     }
     catch (Exception ex)
     {
         Log.Error("Database migration failed", ex);
         throw;
     }
 }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = _configuration["MsSqlConnection:MsSqlConnectionString"];

            services.AddDbContext <MsSQLContext>(options => options.UseSqlServer(connectionString));

            if (_environment.IsDevelopment())
            {
                try
                {
                    var evolveConnection = new SqlConnection(connectionString);
                    var evolve           = new Evolve.Evolve(evolveConnection, msg => _logger.LogInformation(msg))
                    {
                        Locations       = new[] { "Backend/db/migrations" },
                        IsEraseDisabled = true,
                    };

                    evolve.Migrate();
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("Database migration failed.", ex);
                    throw;
                }
            }

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            //Dependency Injection
            services.AddScoped <IPatientBusiness, PatientBusinessImpl>();
            services.AddScoped <IPatientRepository, PatientRepositoryImpl>();

            services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>));


            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = _configuration["MySqlConnection:MySqlConnectionString"];

            services.AddDbContext <MySQLContext>(options => options.UseMySql(connectionString));

            if (_environment.IsDevelopment())
            {
                try
                {
                    var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);

                    var evolve = new Evolve.Evolve("evolve.json", evolveConnection, msg => _logger.LogInformation(msg))
                    {
                        Locations = new List <string> {
                            "db/migrations"
                        },
                        IsEraseDisabled = true,
                    };

                    evolve.Migrate();
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("Database migration failed.", ex);
                    throw;
                }
            }

            services.AddMvc();

            services.AddApiVersioning(option => option.ReportApiVersions = true);

            //Dependency Injection
            services.AddScoped <IPersonBusiness, PersonBusinessImpl>();
            services.AddScoped <IBookBusiness, BookBusinessImpl>();

            services.AddScoped <IPersonRepository, PersonRepositoryImpl>();

            services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>));
        }
Beispiel #12
0
        private void MigrateDataBase(string conn)
        {
            try
            {
                MySql.Data.MySqlClient.MySqlConnection evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(conn);
                Evolve.Evolve evolve = new Evolve.Evolve(evolveConnection, (msg) => { Log.Information(msg); })
                {
                    Locations = new List <string>()
                    {
                        "DB/Migrations", "DB/Dataset"
                    },
                    IsEraseDisabled = true
                };

                evolve.Migrate();
            }catch (Exception ex)
            {
                Log.Error("Erro on Migrate database", ex.Message);
                throw;
            }
        }
        public void ConfigureServices(IServiceCollection services)
        {
            try
            {
                var cnx    = new MySqlConnection($"Server=127.0.0.1;Port=3306;Database=evolve;Uid=root;Pwd=;SslMode=none;");
                var evolve = new Evolve.Evolve("evolve.json", cnx, msg => _logger.LogInformation(msg)) // retrieve the MSBuild configuration
                {
                    Locations = new List <string> {
                        "db/migrations"
                    },                      // exclude db/datasets from production environment
                    IsEraseDisabled = true, // ensure erase command is disabled in production
                };

                evolve.Migrate();
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Database migration failed.", ex);
                throw;
            }
        }
Beispiel #14
0
        /// <summary>
        /// Configure and run migrations
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="migrationLocation"></param>
        public static void Configure(string connectionString, string migrationLocation)
        {
            try
            {
                var cnx = new SqlConnection(connectionString);
                //var cnx = new SqliteConnection(Configuration.GetConnectionString("MyDatabase"));
                var evolve = new Evolve.Evolve(cnx, msg => Console.WriteLine(msg))
                {
                    Locations       = new[] { migrationLocation },
                    IsEraseDisabled = true,
                };

                evolve.Migrate();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

                throw;
            }
        }
Beispiel #15
0
        public async Task RunMigrations(bool repair)
        {
            await using var connection = await Obtain();

            var evolve = new Evolve.Evolve(connection, msg => _logger.Information("Evolve: {Message}", msg))
            {
                Locations                    = new[] { Path.Join(Path.GetDirectoryName(typeof(Database).Assembly.Location), "Schema") },
                IsEraseDisabled              = true,
                SqlMigrationPrefix           = "v",
                SqlRepeatableMigrationPrefix = "r",
                CommandTimeout               = 9999999 // some of these are really long!
            };

            if (repair)
            {
                evolve.Repair();
            }

            // Evolve isn't async >.>
            await Task.Run(() => evolve.Migrate());
        }
Beispiel #16
0
        private void Migrate(string path)
        {
            using var connection = new NpgsqlConnection(configuration.GetConnectionString("default"));
            var evolve = new Evolve.Evolve(connection, msg => logger.LogInformation(msg))
            {
                Locations = new[]
                {
                    Path.Combine(path, "db/migrations"),
                    Path.Combine(path, "db/datasets")
                },

                IsEraseDisabled = this.environment.IsProduction()
            };

            if (this.environment.IsDevelopment())
            {
                evolve.Erase();
            }

            evolve.Migrate();
        }
        public static void Migrate(string connectionString, ILogger logger)
        {
            try
            {
                var connection = new NpgsqlConnection(connectionString);
                var evolve     = new Evolve.Evolve(connection, message => logger.LogInformation(message))
                {
                    Locations = new List <string> {
                        "db/migrations"
                    },
                    IsEraseDisabled = true,
                };

                evolve.Migrate();
            }
            catch (Exception ex)
            {
                logger.LogCritical("Database migration failed.", ex);
                throw;
            }
        }
Beispiel #18
0
        public Action <IApplicationBuilder> Configure(Action <IApplicationBuilder> next)
        {
            try
            {
                var cnx    = new NpgsqlConnection(_configuration.GetConnectionString("MyDataDb"));
                var evolve = new Evolve.Evolve(cnx, message => _logger.LogInformation(message))
                {
                    Locations       = new[] { "Migration/Postgres" },
                    IsEraseDisabled = true,
                };

                evolve.Migrate();
            }
            catch (Exception exception)
            {
                _logger.LogCritical("Database migration failed.", exception);
                throw;
            }

            return(next);
        }
Beispiel #19
0
 private void ExecuteMigrations(string connectionString)
 {
     if (_environment.IsDevelopment())
     {
         try
         {
             var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
             var evolve           = new Evolve.Evolve(evolveConnection, msg => _logger.LogInformation(msg))
             {
                 Locations       = new[] { "db/migrations" },
                 IsEraseDisabled = true,
             };
             evolve.Migrate();
         }
         catch (Exception ex)
         {
             _logger.LogCritical("Database migration failed", ex);
             throw;
         }
     }
 }
Beispiel #20
0
        private void MigrateDatabase(IDbConnection connection)
        {
            try
            {
                var evolve = new Evolve.Evolve(connection)
                {
                    EmbeddedResourceAssemblies = new[] { Assembly.GetExecutingAssembly() },
                    IsEraseDisabled            = true,
                    EnableClusterMode          = false
                };

                evolve.Migrate();

                MigrationDone();
            }
            catch (Exception ee)
            {
                _logger.LogError(ee, "Database migration failed");
                throw;
            }
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = _configuration["MySqlConnection:MySqlConnectionString"];

            services.AddDbContext <MySqlContext>(options => options.UseMySql(connectionString));

            if (_enviroment.IsDevelopment())
            {
                try
                {
                    var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);

                    var evolve = new Evolve.Evolve(evolveConnection, msg => _logger.LogInformation(msg))
                    {
                        Locations = new List <string> {
                            "db/migrations"
                        },
                        IsEraseDisabled = true,
                        Placeholders    = new Dictionary <string, string>
                        {
                            ["${table4}"] = "table4"
                        }
                    };

                    evolve.Migrate();
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("Database migration failed.", ex);
                    throw;
                }
            }

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddApiVersioning();

            services.AddScoped <IPersonService, PersonServiceImpl>();
            services.AddScoped <IPersonRepository, PersonRepositoryImpl>();
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = configuration["MySqlConnection:MySqlConnectionString"];

            services.AddDbContext <MySQLContext>(options => options.UseMySql(connectionString));

            if (this.environment.IsDevelopment())
            {
                try
                {
                    var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
                    var evolve           = new Evolve.Evolve("evolve.json", evolveConnection, msg => logger.LogInformation(msg))
                    {
                        // Where are the magrations:
                        Locations = new List <string> {
                            "Database/migrations"
                        },
                        // Does not clean database
                        IsEraseDisabled = true
                    };
                    evolve.Migrate();
                } catch (Exception exception)
                {
                    this.logger.LogCritical("Database migration failed: ", exception);
                    throw;
                }
            }

            services.AddMvc();

            services.AddApiVersioning();

            /* Dependency Injection:
             *
             * This mapping between the interface and the concrete type defines,
             * that everytime you request a type of IPersonService, you'll get a new instance of the PersonServiceImpl.
             */
            services.AddScoped <IPersonService, PersonServiceImpl>();
            services.AddScoped <IPersonRepository, PersonRepositoryImpl>();
        }
Beispiel #23
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Database connection
            var connectionString = _configuration["MySqlConnection:MySqlConnectionString"];

            services.AddDbContext <MySQLContext>(options => options.UseMySql(connectionString));

            // Envolve Migration Connection
            if (_hostingEnvironment.IsDevelopment())
            {
                try
                {
                    var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
                    var evolve           = new Evolve.Evolve(evolveConnection, msg => _logger.LogInformation(msg))
                    {
                        Locations = new List <string> {
                            string.Concat("v", ApiVersion.Default.MajorVersion, "/db/migrations"),
                            string.Concat("v", ApiVersion.Default.MajorVersion, "/db/dataset"),
                        },
                        IsEraseDisabled = true,
                    };
                    evolve.Migrate();
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("Database migration connection failed.", ex);
                    throw;
                }
            }

            // Default .NET MVC
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            // API Versioning
            services.AddApiVersioning();

            // Dependency Injection
            services.AddScoped <IPersonService, PersonServiceImpl>();
            services.AddScoped <IPersonRepository, PersonRepositoryImpl>();
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = _configuration["MySqlConnection:MySqlConnectionString"];

            services.AddDbContext <MySQLContext>(options => options.UseMySql(connectionString));

            if (_environment.IsDevelopment())
            {
                try
                {
                    var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
                    var evolve           = new Evolve.Evolve("evolve.json", evolveConnection, msg => _logger.LogInformation(msg))
                    {
                        Locations = new List <string> {
                            "db/migrations"
                        },
                        //Not erase data when to run migrations
                        IsEraseDisabled = true,
                    };
                    evolve.Migrate();
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("Database migration failed", ex.Message);
                    throw;
                }
            }

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);


            //Configure version Api -> https://github.com/microsoft/aspnet-api-Versioning/wiki/Versioning-via-the-URL-Path
            services.AddApiVersioning(opttion => opttion.ReportApiVersions = true);

            //Dependency injection
            services.AddScoped <IPersonBusiness, PersonBusinessImpl>();
            services.AddScoped <IPersonRepository, PersonRepositorympl>();
            services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>));
        }
Beispiel #25
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = Configuration["MySqlConnection:MySqlConnectionString"];

            services.AddDbContext <MySqlContext>(options => options.UseMySql(connectionString));

            // Verificando em que ambiente esta.
            // O versionamento de código, ocorrerá somente em ambiente de dev
            if (Environment.IsDevelopment())
            {
                try
                {
                    var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
                    var evolve           = new Evolve.Evolve(evolveConnection, msg => _logger.LogInformation(msg))
                    {
                        Locations = new List <string> {
                            "db/migrations"
                        },
                        IsEraseDisabled = true,
                    };

                    evolve.Migrate();
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("Database migration failed.", ex);
                    throw;
                }
            }

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddScoped <IPersonRepository, PersonRepository>();
            services.AddScoped <IPersonBusiness, PersonBusiness>();

            services.AddScoped <IBookBusiness, BookBusiness>();

            services.AddScoped(typeof(IGenerciRepository <>), typeof(GenerciRepository <>));
        }
Beispiel #26
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //adding DB connections settings here - start
            var connectionString = Configuration["SqlConnection:SqlConnectionString"];

            services.AddDbContext <SQLContext>(options => options.UseSqlServer(connectionString));
            //adding DB connections settings here - end

            if (Environment.IsDevelopment())
            {
                try
                {
                    var evolveConnection = new System.Data.SqlClient.SqlConnection(connectionString);
                    var evolve           = new Evolve.Evolve("evolve.json", evolveConnection, msg => _logger.LogInformation(msg))
                    {
                        Locations = new List <string> {
                            "db/migrations"
                        },
                        IsEraseDisabled = true
                    };
                    evolve.Migrate();
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("Database Migration failed", ex);
                    throw;
                }
            }

            services.AddMvc();
            services.AddApiVersioning();
            //dependency injection -- Add all services here, then, add a ref of the service into the controller - start
            services.AddScoped <IPersonBusiness, PersonBusinessImpl>();
            services.AddScoped <IBookBusiness, BookBusinessImpl>();

            //services.AddScoped<IPersonRepository, PersonRepositoryImpl>();
            services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>));
            //dependency injection -- Add all services here, then, add a ref of the service into the controller - end
        }
Beispiel #27
0
        public static void InitializeDatabase()
        {
            if (!string.IsNullOrEmpty(OrganisationRegistryProperties.GetInstance().DBConnectionString))
            {
                using (DbConnection connection = DaoUtil.GetConnection())
                {
                    try
                    {
                        string location = OrganisationRegistryProperties.GetInstance().MigrationScriptsPath;

                        if (OrganisationRegistryProperties.GetInstance().Database.Equals(DatabaseType.MSSQL))
                        {
                            var evolve = new Evolve.Evolve(connection, msg => log.Info(msg))
                            {
                                Locations       = new[] { location },
                                Schemas         = new[] { "dbo" }, // default schema can be NULL in SQL Server, which makes Evolve unhappy
                                IsEraseDisabled = true
                            };

                            evolve.Migrate();
                        }
                        else
                        {
                            var evolve = new Evolve.Evolve(connection, msg => log.Info(msg))
                            {
                                Locations       = new[] { location },
                                IsEraseDisabled = true
                            };

                            evolve.Migrate();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = _configuration.GetConnectionString("PgSqlConnectionString");

            services.AddDbContext <PgSQLContext>(options => options.UseNpgsql(connectionString));

            if (_environment.IsDevelopment())
            {
                try
                {
                    var evolveConnection = new Npgsql.NpgsqlConnection(connectionString);
                    var evolve           = new Evolve.Evolve(evolveConnection, msg => _logger.LogInformation(msg))
                    {
                        Locations = new List <string> {
                            "db"
                        },
                        IsEraseDisabled = true
                    };

                    evolve.Migrate();
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("Database migration failed.", ex);
                    throw;
                }
            }
            // Add framework services.
            services.AddMvc();

            services.AddApiVersioning();

            // Dependency Injection
            services.AddScoped <IPersonService, PersonService>();
            services.AddScoped <IBookService, BookService>();
            services.AddScoped <IPersonRepository, PersonRepository>();

            services.AddScoped(typeof(IRepository <>), typeof(GenericRepository <>));
        }
        private void ExecuteMigrations(string connectionString)
        {
            if (iEnvironment.IsDevelopment())
            {
                try {
                    var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
                    var evolve           = new Evolve.Evolve(evolveConnection, msg => iLogger.LogInformation(msg))
                    {
                        Locations = new List <string> {
                            "db/migrations", "db/dataset"
                        },
                        IsEraseDisabled = true,
                    };

                    evolve.Migrate();
                }
                catch (Exception ex) {
                    iLogger.LogCritical("Banco de Dados migration failed.", ex);
                    throw;
                }
            }
        }
Beispiel #30
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = _configuration["MySqlConnection:MySqlConnectionString"];

            services.AddDbContext <MySqlContext>(options => options.UseMySql(connectionString));

            if (_environment.IsDevelopment())
            {
                try
                {
                    var evolveConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);

                    var evolve = new Evolve.Evolve("evolve.json", evolveConnection, msg => _logger.LogInformation(msg))
                    {
                        Locations = new List <string> {
                            "db/migrations"
                        },
                        IsEraseDisabled = true // não permite apagar os dados ao realizar alteração na base de dados.
                    };

                    evolve.Migrate();
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("Database migration failed!", ex);

                    throw;
                }
            }

            // Add framework services
            services.AddMvc();

            services.AddApiVersioning();

            // Add dependency injection
            services.AddScoped <IPersonService, PersonService>();
            services.AddScoped <IPersonRepository, PersonRepository>();
        }