Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var context     = services.GetRequiredService <ApplicationDbContext>();
                    var usermanager = services.GetRequiredService <UserManager <AppUser> >();
                    var rolemanager = services.GetRequiredService <RoleManager <IdentityRole> >();
                    SeedData.Initialize(context, usermanager, rolemanager).Wait();
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while seeding the database.");
                }
            }

            host.Run();
        }
Exemplo n.º 2
0
        //public static IWebHost BuildWebHost(string[] args) =>
        //    WebHost.CreateDefaultBuilder(args)
        //        .UseStartup<Startup>()
        //        .Build();

        public static IWebHost BuildWebHost(string[] args)
        {
            var host = WebHost.CreateDefaultBuilder(args)
                       .UseStartup <Startup>()
                       .UseUrls(urls: "http://0.0.0.0:5000")
                       .Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                var context  = scope.ServiceProvider.GetService <Context>();

                try
                {
                    context.Database.Migrate();
                    SeedData.Initialize(context);
                } catch (Exception e)
                {
                    // something
                }
            }

            return(host);
        }