using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
namespace Elsa.EntityFrameworkCore.Common;
///
/// Executes EF Core migrations using the specified type.
///
public class RunMigrationsHostedService : IHostedService where TDbContext : DbContext
{
private readonly IDbContextFactory _dbContextFactory;
///
/// Initializes a new instance of the class.
///
public RunMigrationsHostedService(IDbContextFactory dbContextFactoryFactory) => _dbContextFactory = dbContextFactoryFactory;
///
public async Task StartAsync(CancellationToken cancellationToken)
{
await using var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
await dbContext.Database.MigrateAsync(cancellationToken);
await dbContext.DisposeAsync();
}
///
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}