public UserControllerTest()
        {
            _dbOptions = new DbContextOptionsBuilder <UserContext>()
                         .UseInMemoryDatabase(databaseName: "in-memory")
                         .Options;

            using (var dbContext = new UserContext(_dbOptions))
            {
                dbContext.AddRange(UserContextDbSeed.GetData());
                dbContext.SaveChanges();
            }
        }
Example #2
0
        private static void CreateDbIfNotExists(IWebHost host)
        {
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var context = services.GetRequiredService <UserContext>();
                    UserContextDbSeed.Seed(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred creating the DB.");
                }
            }
        }