public static async Task Main(string[] args) { IHost host = CreateHostBuilder(args).Build( ); await PostgresSwapper .ManageDataAsync(host) .ConfigureAwait(false); await host .RunAsync( ) .ConfigureAwait(false); using IServiceScope scope = host.Services.CreateScope( ); IServiceProvider services = scope.ServiceProvider; ILoggerFactory loggerFactory = services.GetRequiredService <ILoggerFactory>( ); try { ApplicationDbContext context = services.GetRequiredService <ApplicationDbContext>( ); UserManager <HeimdallUser> userManager = services.GetRequiredService <UserManager <HeimdallUser> >( ); RoleManager <IdentityRole> roleManager = services.GetRequiredService <RoleManager <IdentityRole> >( ); await ContextSeed.RunSeedMethodsAsync(context, roleManager, userManager).ConfigureAwait(false); } catch (Exception ex) { ILogger <Program> logger = loggerFactory.CreateLogger <Program>( ); logger.LogError(ex, "An error occurred seeding the DB."); } await host.RunAsync( ).ConfigureAwait(false); }
public static async Task ManageDataAsync(IHost host) { try { // This technique is used to obtain references to services using IServiceScope svcScope = host.Services.CreateScope( ); IServiceProvider svcProvider = svcScope.ServiceProvider; // Seed Data IServiceProvider services = svcScope.ServiceProvider; ApplicationDbContext context = services.GetRequiredService <ApplicationDbContext>( ); UserManager <HeimdallUser> userManager = services.GetRequiredService <UserManager <HeimdallUser> >( ); RoleManager <IdentityRole> roleManager = services.GetRequiredService <RoleManager <IdentityRole> >( ); await ContextSeed.RunSeedMethodsAsync(context, roleManager, userManager).ConfigureAwait(false); // The service will run your migrations ApplicationDbContext dbContextSvc = svcProvider.GetRequiredService <ApplicationDbContext>( ); await dbContextSvc.Database.MigrateAsync( ).ConfigureAwait(false); } catch (Exception ex) { Console.WriteLine($"Exception while running Manage Data -> {ex}"); } }
public async static Task SeedDataAsync(IHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var loggerFactory = services.GetRequiredService <ILoggerFactory>(); try { var context = services.GetRequiredService <ApplicationDbContext>(); var userManager = services.GetRequiredService <UserManager <FpUser> >(); var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >(); var configuration = services.GetRequiredService <IConfiguration>(); var fileService = services.GetRequiredService <IFP_FileService>(); await ContextSeed.RunSeedMethodsAsync(roleManager, userManager, context, configuration, fileService); } catch (Exception ex) { var logger = loggerFactory.CreateLogger <Program>(); logger.LogError(ex, "An error occured seeding the DB"); } } }
public static async Task ManageData(IHost host) { try { using var svcScope = host.Services.CreateScope(); var svcProvider = svcScope.ServiceProvider; var services = svcScope.ServiceProvider; var context = services.GetRequiredService <ApplicationDbContext>(); var userManager = services.GetRequiredService <UserManager <FpUser> >(); var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >(); var configuration = services.GetRequiredService <IConfiguration>(); var fileService = services.GetRequiredService <IFP_FileService>(); await context.Database.MigrateAsync(); await ContextSeed.RunSeedMethodsAsync(roleManager, userManager, context, configuration, fileService); } catch (Exception ex) { Console.WriteLine($"Exception while running Manage Data => {ex}"); } }