// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); IConfiguration configuration = builder.Build(); string connectionString = configuration.GetConnectionString("SaveItContext"); services.AddDbContext <SaveItContext>(SaveItContext.UseMySql(connectionString)); // Add framework services. services.AddMvc(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseMvc(); if (env.IsDevelopment()) { // If in development, ensure that the database is created. IServiceScopeFactory serviceScopeFactory = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>(); using (IServiceScope serviceScope = serviceScopeFactory.CreateScope()) { SaveItContext context = serviceScope.ServiceProvider.GetService <SaveItContext>(); context.Database.EnsureCreated(); } } }