Files
vegasco/src/Vegasco.Server.Api/Persistence/ApplyMigrationsService.cs

18 lines
683 B
C#
Raw Normal View History

2024-08-24 13:43:43 +02:00
using Microsoft.EntityFrameworkCore;
namespace Vegasco.Server.Api.Persistence;
2024-08-24 13:43:43 +02:00
public class ApplyMigrationsService(ILogger<ApplyMigrationsService> logger, IServiceScopeFactory scopeFactory)
: IHostedService
2024-08-24 13:43:43 +02:00
{
public async Task StartAsync(CancellationToken cancellationToken)
{
logger.LogInformation("Starting migrations");
using IServiceScope scope = scopeFactory.CreateScope();
2024-08-24 13:43:43 +02:00
await using var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await dbContext.Database.MigrateAsync(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}