< Summary

Information
Class: Elsa.Persistence.EFCore.Abstractions.DesignTimeDbContextFactoryBase<T>
Assembly: Elsa.Persistence.EFCore.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/Abstractions/DesignTimeDbContextFactoryBase.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 17
Coverable lines: 17
Total lines: 46
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
CreateDbContext(...)0%620%
ConfigureServices(...)100%210%

File(s)

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

#LineLine coverage
 1using System.CommandLine;
 2using Microsoft.EntityFrameworkCore;
 3using Microsoft.EntityFrameworkCore.Design;
 4using Microsoft.Extensions.DependencyInjection;
 5
 6namespace Elsa.Persistence.EFCore.Abstractions;
 7
 8/// <summary>
 9/// A design-time factory base class that can be inherited from by provider-specific implementations.
 10/// </summary>
 11public abstract class DesignTimeDbContextFactoryBase<TDbContext> : IDesignTimeDbContextFactory<TDbContext> where TDbCont
 12{
 13    /// <inheritdoc />
 14    public TDbContext CreateDbContext(string[] args)
 15    {
 016        var builder = new DbContextOptionsBuilder<TDbContext>();
 017        var connectionStringOption = new Option<string>("--connectionString")
 018        {
 019            Description = "Specifies the connection string."
 020        };
 021        var command = new RootCommand
 022        {
 023            connectionStringOption
 024        };
 25
 026        var parseResult = command.Parse(args);
 027        var connectionString = parseResult.GetValue(connectionStringOption) ?? "Data Source=local";
 028        var services = new ServiceCollection();
 29
 030        ConfigureServices(services);
 031        ConfigureBuilder(builder, connectionString);
 32
 033        var serviceProvider = services.BuildServiceProvider();
 034        return (TDbContext)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TDbContext), builder.Options);
 35    }
 36
 37    protected virtual void ConfigureServices(IServiceCollection services)
 38    {
 39        // This method can be overridden to configure additional services if needed.
 040    }
 41
 42    /// <summary>
 43    /// Implement this to configure the <see cref="DbContextOptionsBuilder{TContext}"/>.
 44    /// </summary>
 45    protected abstract void ConfigureBuilder(DbContextOptionsBuilder<TDbContext> builder, string connectionString);
 46}