< 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: 14
Coverable lines: 14
Total lines: 43
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", "Specifies the connection string.");
 018        var command = new RootCommand
 019        {
 020            connectionStringOption
 021        };
 22
 023        var parseResult = command.Parse(args);
 024        var connectionString = parseResult.GetValue(connectionStringOption) ?? "Data Source=local";
 025        var services = new ServiceCollection();
 26
 027        ConfigureServices(services);
 028        ConfigureBuilder(builder, connectionString);
 29
 030        var serviceProvider = services.BuildServiceProvider();
 031        return (TDbContext)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TDbContext), builder.Options);
 32    }
 33
 34    protected virtual void ConfigureServices(IServiceCollection services)
 35    {
 36        // This method can be overridden to configure additional services if needed.
 037    }
 38
 39    /// <summary>
 40    /// Implement this to configure the <see cref="DbContextOptionsBuilder{TContext}"/>.
 41    /// </summary>
 42    protected abstract void ConfigureBuilder(DbContextOptionsBuilder<TDbContext> builder, string connectionString);
 43}