Beispiel #1
0
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureServices(services =>
            {
                services.AddEntityFrameworkInMemoryDatabase();

                var provider = services
                               .AddEntityFrameworkInMemoryDatabase()
                               .BuildServiceProvider();

                services.AddDbContext <SiteContext>(options =>
                {
                    options.UseInMemoryDatabase("InMemoryDbForTesting");
                    options.UseInternalServiceProvider(provider);
                });

                var sp = services.BuildServiceProvider();

                using (var scope = sp.CreateScope())
                {
                    var scopedServices = scope.ServiceProvider;
                    var db             = scopedServices.GetRequiredService <SiteContext>();
                    var loggerFactory  = scopedServices.GetRequiredService <ILoggerFactory>();

                    var logger = scopedServices
                                 .GetRequiredService <ILogger <CustomWebApplicationFactory> >();
                    db.Database.EnsureDeleted();
                    db.Database.EnsureCreated();

                    try
                    {
                        DataBaseSeed.SeedShopAsync(db).Wait();
                        var userManager = scopedServices.GetRequiredService <UserManager <IdentityUser> >();
                        var roleManager = scopedServices.GetRequiredService <RoleManager <IdentityRole> >();
                        DataBaseSeed.SeedIdentityAsync(userManager, roleManager).Wait();
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"An error occurred seeding the " +
                                        "database with test messages. Error: {ex.Message}");
                    }
                }
            });
        }
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                         .Enrich.FromLogContext()
                         .WriteTo.File("log.txt", rollingInterval: RollingInterval.Hour)
                         .CreateLogger();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var siteContext = services.GetRequiredService <SiteContext>();
                    if (siteContext.Database.EnsureCreated())
                    {
                        await DataBaseSeed.SeedShopAsync(siteContext);
                    }
                    if (!siteContext.Users.Any())
                    {
                        var userManager = services.GetRequiredService <UserManager <IdentityUser> >();
                        var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();
                        await DataBaseSeed.SeedIdentityAsync(userManager, roleManager);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Cant create users");
                }
            }
            host.Run();
        }