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

                // Create a new service provider.
                var provider = services
                               .AddEntityFrameworkInMemoryDatabase()
                               .BuildServiceProvider();

                // Add a database context (ApplicationDbContext) using an in-memory
                // database for testing.
                //services.AddDbContext<NetlogContext>(options =>
                //{
                //    options.UseInMemoryDatabase("Netlog");
                //    options.UseInternalServiceProvider(provider);
                //});

                services.AddDbContext <NetlogContext>(c =>
                                                      c.UseNpgsql("User ID=testcase27;Password=Def73c6B;Server=212.237.5.27;Port=5432;Database=testcasedb27;Integrated Security=true;Pooling=true;"), ServiceLifetime.Singleton);


                // Build the service provider.
                var sp = services.BuildServiceProvider();

                // Create a scope to obtain a reference to the database
                // context (ApplicationDbContext).
                using (var scope = sp.CreateScope())
                {
                    var scopedServices = scope.ServiceProvider;
                    var db             = scopedServices.GetRequiredService <NetlogContext>();
                    var loggerFactory  = scopedServices.GetRequiredService <ILoggerFactory>();

                    var logger = scopedServices
                                 .GetRequiredService <ILogger <CustomWebApplicationFactory <TStartup> > >();

                    // Ensure the database is created.
                    db.Database.EnsureCreated();

                    try
                    {
                        // Seed the database with test data.
                        NetlogContextSeed.SeedAsync(db, loggerFactory).Wait();
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"An error occurred seeding the " +
                                        "database with test messages. Error: {ex.Message}");
                    }
                }
            });
        }
Example #2
0
        private static void SeedDatabase(IWebHost host)
        {
            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();

                try
                {
                    var NetlogContext = services.GetRequiredService <NetlogContext>();
                    NetlogContextSeed.SeedAsync(NetlogContext, loggerFactory).Wait();
                }
                catch (Exception exception)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(exception, "An error occurred seeding the DB.");
                }
            }
        }