コード例 #1
0
        public CatDataServiceTest()
        {
            var options = new DbContextOptionsBuilder <CatDbContext>()
                          .UseInMemoryDatabase(databaseName: "CatVotes")
                          .Options;

            context = new CatDbContext(options);
            CatDbInitializer.Initialize(context);

            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .AddFilter("Microsoft", LogLevel.Warning)
                .AddFilter("System", LogLevel.Warning)
                .AddFilter("LoggingConsoleApp.Program", LogLevel.Debug);
            });

            logger = loggerFactory.CreateLogger <CatsDataService>();
        }
コード例 #2
0
        private static void CreateDbIfNotExists(IHost host)
        {
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var context = services.GetRequiredService <CatDbContext>();
                    context.Database.EnsureCreated();
                    CatDbInitializer.Initialize(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred creating the DB.");
                }
            }
        }