feat(server): add database context and migrations
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GroundsForSupport.Server.Data;
|
||||
|
||||
internal sealed class MigrationService(
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<MigrationService> logger
|
||||
) : IHostedService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider = serviceProvider;
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
logger.LogInformation("Applying database migrations...");
|
||||
|
||||
using var scope = _serviceProvider.CreateScope();
|
||||
var context = scope.ServiceProvider.GetRequiredService<Context>();
|
||||
await context.Database.MigrateAsync(cancellationToken);
|
||||
|
||||
logger.LogInformation("Database migrations applied.");
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user