class Program
{
public async static Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
// var sqlContext = CreateSqlDbContext(args);
var mysqlContext = CreateMySqlDbContext(args);
//Delete existing mysql database
Console.WriteLine("DELETING EXISTING MYSQL DATABASE");
await mysqlContext.Database.EnsureDeletedAsync();
//create new mysql database
Console.WriteLine("CREATING NEW MYSQL DATABASE");
await mysqlContext.Database.EnsureCreatedAsync();
//copy data from sql to mysql
Console.WriteLine("COPYING TABLE DATA FROM SQL TO MYSQL");
var dataMigratorService = host.Services.GetRequiredService<DataMigratorService>();
await dataMigratorService.DoDataCopy();
//Stored proc builder service to be added shortly
}
public static ApplicationDbContext CreateSqlDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
.AddEnvironmentVariables()
.Build();
var connectionString = configuration.GetConnectionString("SQLConnection");
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseSqlServer(connectionString);
return new ApplicationDbContext(optionsBuilder.Options);
}
public static MySqlDbContext CreateMySqlDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
.AddEnvironmentVariables()
.Build();
var connectionString = configuration.GetConnectionString("MySQLConnection");
var optionsBuilder = new DbContextOptionsBuilder<MySqlDbContext>()
.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
return new MySqlDbContext(optionsBuilder.Options);
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
// register your services here.
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
.AddEnvironmentVariables()
.Build();
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
configuration.GetConnectionString("SQLConnection"),
b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)));
services.AddDbContext<MySqlDbContext>(options =>
options.UseMySql(configuration.GetConnectionString("MySQLConnection"), new MySqlServerVersion(new Version())));
services.AddScoped<IApplicationDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
services.AddScoped<IMySqlDbContext>(provider => provider.GetRequiredService<MySqlDbContext>());
services.AddTransient<DataMigratorService>();
});
}