Exemplo n.º 1
0
        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 can't 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();

                // Seed Data
                var services    = svcScope.ServiceProvider;
                var context     = services.GetRequiredService <ApplicationDbContext>();
                var usermanager = services.GetRequiredService <UserManager <BTUser> >();
                var rolemanager = services.GetRequiredService <RoleManager <IdentityRole> >();
                await ContextSeed.SeedRolesAsync(rolemanager);

                await ContextSeed.SeedDefaultUsersAsync(usermanager);

                await ContextSeed.SeedDefaultTicketPropsAsync(context);

                await ContextSeed.SeedProjectsAsync(context);

                await ContextSeed.SeedProjectUsersAsync(context, usermanager);

                await ContextSeed.SeedTicketsAsync(context, usermanager);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception while running Manage Data -> {ex}");
            }
        }