| | | 1 | | using System.CommandLine; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using Microsoft.EntityFrameworkCore.Design; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | |
| | | 6 | | namespace 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> |
| | | 11 | | public abstract class DesignTimeDbContextFactoryBase<TDbContext> : IDesignTimeDbContextFactory<TDbContext> where TDbCont |
| | | 12 | | { |
| | | 13 | | /// <inheritdoc /> |
| | | 14 | | public TDbContext CreateDbContext(string[] args) |
| | | 15 | | { |
| | 0 | 16 | | var builder = new DbContextOptionsBuilder<TDbContext>(); |
| | 0 | 17 | | var connectionStringOption = new Option<string>("--connectionString", "Specifies the connection string."); |
| | 0 | 18 | | var command = new RootCommand |
| | 0 | 19 | | { |
| | 0 | 20 | | connectionStringOption |
| | 0 | 21 | | }; |
| | | 22 | | |
| | 0 | 23 | | var parseResult = command.Parse(args); |
| | 0 | 24 | | var connectionString = parseResult.GetValue(connectionStringOption) ?? "Data Source=local"; |
| | 0 | 25 | | var services = new ServiceCollection(); |
| | | 26 | | |
| | 0 | 27 | | ConfigureServices(services); |
| | 0 | 28 | | ConfigureBuilder(builder, connectionString); |
| | | 29 | | |
| | 0 | 30 | | var serviceProvider = services.BuildServiceProvider(); |
| | 0 | 31 | | 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. |
| | 0 | 37 | | } |
| | | 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 | | } |