| | | 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") |
| | 0 | 18 | | { |
| | 0 | 19 | | Description = "Specifies the connection string." |
| | 0 | 20 | | }; |
| | 0 | 21 | | var command = new RootCommand |
| | 0 | 22 | | { |
| | 0 | 23 | | connectionStringOption |
| | 0 | 24 | | }; |
| | | 25 | | |
| | 0 | 26 | | var parseResult = command.Parse(args); |
| | 0 | 27 | | var connectionString = parseResult.GetValue(connectionStringOption) ?? "Data Source=local"; |
| | 0 | 28 | | var services = new ServiceCollection(); |
| | | 29 | | |
| | 0 | 30 | | ConfigureServices(services); |
| | 0 | 31 | | ConfigureBuilder(builder, connectionString); |
| | | 32 | | |
| | 0 | 33 | | var serviceProvider = services.BuildServiceProvider(); |
| | 0 | 34 | | 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. |
| | 0 | 40 | | } |
| | | 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 | | } |