public static async Task Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var loggerFactory = services.GetRequiredService <ILoggerFactory>(); try { var context = services.GetRequiredService <StoreContext>(); await context.Database.MigrateAsync(); await StoreContextSeed.SeedData(context, loggerFactory); } catch (System.Exception ex) { var logger = loggerFactory.CreateLogger <Program>(); logger.LogError(ex, "An Error occured during migration"); } } host.Run(); }
public static async Task Main(string[] args) { //code below is to configure the startup of our //application to check if there are any updated migrations //and create the DB automatically using the latest migrations var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var loggerFactory = services.GetRequiredService <ILoggerFactory>(); try { var context = services.GetRequiredService <StoreContext>(); await context.Database.MigrateAsync(); //calling seeddata class's static variable for seeding data at the start of the program await StoreContextSeed.SeedData(context, loggerFactory); } catch (Exception ex) { var logger = loggerFactory.CreateLogger <Program>(); logger.LogError(ex, "An error occured during Migration"); } } host.Run(); }
public static async Task Main(string[] args) { // While Starting the Application if the database is not there. // Any Pending migration not carried will be done with below configuration. var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var loggerFactory = services.GetService <ILoggerFactory>(); try { // while program gets started the below code will create the // database Skinet if not there and do the migration if not there var context = services.GetRequiredService <StoreContext>(); await context.Database.MigrateAsync(); await StoreContextSeed.SeedData(context, loggerFactory); // while program gets started the below code will create the // database Identity if not available along with migration var userManager = services.GetRequiredService <UserManager <AppUser> >(); var identityContext = services.GetRequiredService <AppIdentityDBContext>(); await identityContext.Database.MigrateAsync(); await AppIdentityContextSeed.SeedUserAsync(userManager); } catch (Exception ex) { var logger = loggerFactory.CreateLogger <Program>(); logger.LogError(ex, "Failed to migrate"); } } host.Run(); }