public static async Task ManageData(IHost host) { try { // This technique is used to obtain references to services // normally I would just inject these services but you cant use a constructor in a static class using var svcScope = host.Services.CreateScope(); var svcProvider = svcScope.ServiceProvider; //The service will run your migrations var dbContextSvc = svcProvider.GetRequiredService <ApplicationDbContext>(); await dbContextSvc.Database.MigrateAsync(); var services = svcScope.ServiceProvider; var context = services.GetRequiredService <ApplicationDbContext>(); var userManager = services.GetRequiredService <UserManager <FPUser> >(); var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >(); var fileService = services.GetRequiredService <IFPFileService>(); var configuration = services.GetRequiredService <IConfiguration>(); await ContextSeed.SeedDataBaseAsync(context, userManager, roleManager, fileService, configuration); } catch (Exception ex) { Console.WriteLine($"Exception while running Manage Data => {ex}"); } }
public static async Task ManageData(IHost host) { try { using var svcScope = host.Services.CreateScope(); var svcProvider = svcScope.ServiceProvider; var dbContextSvc = svcProvider.GetRequiredService <ApplicationDbContext>(); await dbContextSvc.Database.MigrateAsync(); var services = svcScope.ServiceProvider; var context = services.GetRequiredService <ApplicationDbContext>(); var userManager = services.GetRequiredService <UserManager <FAUser> >(); var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >(); var fileService = services.GetRequiredService <IAvatarService>(); await ContextSeed.SeedDataBaseAsync(context, userManager, roleManager, fileService); } catch (Exception ex) { Console.WriteLine($"Exception while running Manage Data => {ex}"); } }