Files
vegasco/src/WebApi/Persistence/ApplyMigrationsService.cs

22 lines
716 B
C#
Raw Normal View History

2024-08-24 13:43:43 +02:00
using Microsoft.EntityFrameworkCore;
namespace Vegasco.WebApi.Persistence;
public class ApplyMigrationsService : IHostedService
{
private readonly IServiceScopeFactory _scopeFactory;
public ApplyMigrationsService(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
using IServiceScope scope = _scopeFactory.CreateScope();
await using var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await dbContext.Database.MigrateAsync(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}