private void InitializeSystemWithAdminIfNeeded(IComponentContext context, UserManagementContext db) { var isAmin = db.Users.Any(x => x.Role == Contracts.User.Enums.Role.Admin); if (!isAmin) { var repository = context.Resolve <UserRepository>(); var userFactory = context.Resolve <IUserFactory>(); repository.AddAsync(userFactory.Create("Admin", "Admin", adminEmail, adminPassword, Contracts.User.Enums.Role.Admin)).Wait(); } }
/// <summary> /// Create the database if it doesn't already exist. /// </summary> /// <param name="host"></param> public static void CreateDb(IHost host) { // Get a scope so that we can access services. using var scope = host.Services.CreateScope(); var services = scope.ServiceProvider; try { // Get the database context service. UserManagementContext dbContext = services.GetRequiredService <UserManagementContext>(); UserManager <ApplicationUser> userManager = services.GetRequiredService <UserManager <ApplicationUser> >(); RoleManager <IdentityRole> roleManager = services.GetRequiredService <RoleManager <IdentityRole> >(); // Initialize the database. DbInitializer.Initialize(dbContext, userManager, roleManager); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred creating or initializing the database."); } }