using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Elsa.Persistence.EFCore; /// /// Executes EF Core migrations using the specified type. /// public class RunMigrationsHostedService : IHostedService where TDbContext : DbContext { private readonly IServiceScopeFactory _scopeFactory; /// /// Initializes a new instance of the class. /// public RunMigrationsHostedService(IServiceScopeFactory scopeFactory) { _scopeFactory = scopeFactory; } /// public async Task StartAsync(CancellationToken cancellationToken) { using var scope = _scopeFactory.CreateScope(); var dbContextFactory = scope.ServiceProvider.GetRequiredService>(); await using var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); await dbContext.Database.MigrateAsync(cancellationToken); } /// public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; }