< Summary

Information
Class: Elsa.Persistence.EFCore.RunMigrationsHostedService<T>
Assembly: Elsa.Persistence.EFCore.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/RunMigrationsHostedService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 9
Coverable lines: 9
Total lines: 33
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
StartAsync()0%620%
StopAsync(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/RunMigrationsHostedService.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using Microsoft.Extensions.DependencyInjection;
 3using Microsoft.Extensions.Hosting;
 4
 5namespace Elsa.Persistence.EFCore;
 6
 7/// <summary>
 8/// Executes EF Core migrations using the specified <see cref="DbContext"/> type.
 9/// </summary>
 10public class RunMigrationsHostedService<TDbContext> : IHostedService where TDbContext : DbContext
 11{
 12    private readonly IServiceScopeFactory _scopeFactory;
 13
 14    /// <summary>
 15    /// Initializes a new instance of the <see cref="RunMigrationsHostedService{TDbContext}"/> class.
 16    /// </summary>
 017    public RunMigrationsHostedService(IServiceScopeFactory scopeFactory)
 18    {
 019        _scopeFactory = scopeFactory;
 020    }
 21
 22    /// <inheritdoc />
 23    public async Task StartAsync(CancellationToken cancellationToken)
 24    {
 025        using var scope = _scopeFactory.CreateScope();
 026        var dbContextFactory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<TDbContext>>();
 027        await using var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
 028        await dbContext.Database.MigrateAsync(cancellationToken);
 029    }
 30
 31    /// <inheritdoc />
 032    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
 33}