예제 #1
0
        public static void Main(string[] args)
        {
            CurrentDirectoryHelpers.SetCurrentDirectory();

            var host = CreateWebHostBuilder(args).Build();

            #if !(DEBUG)
            Thread.Sleep(10000); // wait for start of postgres container (cannot understand how to add wait for it script)
            #endif

            using (var scope = host.Services.CreateScope())
            {
                try
                {
                    var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
                    context.Database.Migrate();
                    CoursesInitializer.Initialize(context);
                }
                catch (Exception ex)
                {
                    var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while migrating or initializing the database.");
                }
            }

            host.Run();
        }
        public static ApplicationDbContext Create()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var context = new ApplicationDbContext(options);

            context.Database.EnsureCreated();

            CoursesInitializer.Initialize(context);

            return(context);
        }