Create database at application startup

This commit is contained in:
2020-03-05 19:45:14 +01:00
parent 41187c32d6
commit d8035452d8
2 changed files with 5 additions and 1 deletions

View File

@@ -7,7 +7,6 @@ namespace EstusShots.Server.Services
{ {
public EstusShotsContext(DbContextOptions options) : base(options) public EstusShotsContext(DbContextOptions options) : base(options)
{ {
Database.EnsureCreated();
} }
public DbSet<Season> Seasons { get; set; } = default!; public DbSet<Season> Seasons { get; set; } = default!;

View File

@@ -74,6 +74,11 @@ namespace EstusShots.Server
app.UseRouting(); app.UseRouting();
app.UseAuthorization(); app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
// Open a scope and create the database, if necessary
using var scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
using var context = scope.ServiceProvider.GetService<EstusShotsContext>();
context.Database.EnsureCreated();
} }
} }
} }