| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | | using Microsoft.EntityFrameworkCore.Design; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Persistence.Factories; |
| | 5 | |
|
| | 6 | | using Contexts; |
| | 7 | | using Enums; |
| | 8 | | using Providers; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// This is used for dotnet ef CLI to set up connection for migration stuff |
| | 12 | | /// </summary> |
| | 13 | | public class NLightningContextFactory : IDesignTimeDbContextFactory<NLightningDbContext> |
| | 14 | | { |
| | 15 | | public NLightningDbContext CreateDbContext(string[] args) |
| | 16 | | { |
| 0 | 17 | | var optionsBuilder = new DbContextOptionsBuilder<NLightningDbContext>(); |
| | 18 | |
|
| 0 | 19 | | var postgresString = Environment.GetEnvironmentVariable("NLIGHTNING_POSTGRES"); |
| 0 | 20 | | if (postgresString != null) |
| | 21 | | { |
| 0 | 22 | | optionsBuilder.UseNpgsql(postgresString, x => |
| 0 | 23 | | { |
| 0 | 24 | | x.MigrationsAssembly("NLightning.Infrastructure.Persistence.Postgres"); |
| 0 | 25 | | }) |
| 0 | 26 | | .EnableSensitiveDataLogging() |
| 0 | 27 | | .UseSnakeCaseNamingConvention(); |
| 0 | 28 | | return new NLightningDbContext(optionsBuilder.Options, new DatabaseTypeProvider(DatabaseType.PostgreSql)); |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | var sqlite = Environment.GetEnvironmentVariable("NLIGHTNING_SQLITE"); |
| 0 | 32 | | if (sqlite != null) |
| | 33 | | { |
| 0 | 34 | | optionsBuilder.UseSqlite(sqlite, x => |
| 0 | 35 | | { |
| 0 | 36 | | x.MigrationsAssembly("NLightning.Infrastructure.Persistence.Sqlite"); |
| 0 | 37 | | }); |
| 0 | 38 | | return new NLightningDbContext(optionsBuilder.Options, new DatabaseTypeProvider(DatabaseType.Sqlite)); |
| | 39 | | } |
| | 40 | |
|
| 0 | 41 | | var sqlServer = Environment.GetEnvironmentVariable("NLIGHTNING_SQLSERVER"); |
| 0 | 42 | | if (sqlServer != null) |
| | 43 | | { |
| 0 | 44 | | optionsBuilder.UseSqlServer(sqlServer, x => |
| 0 | 45 | | { |
| 0 | 46 | | x.MigrationsAssembly("NLightning.Infrastructure.Persistence.SqlServer"); |
| 0 | 47 | | }); |
| 0 | 48 | | return new NLightningDbContext(optionsBuilder.Options, new DatabaseTypeProvider(DatabaseType.MicrosoftSql)); |
| | 49 | | } |
| | 50 | |
|
| 0 | 51 | | throw new Exception( |
| 0 | 52 | | "Must set NLIGHTNING_POSTGRES or NLIGHTNING_SQLITE or NLIGHTNING_SQLSERVER env for generation."); |
| | 53 | | } |
| | 54 | | } |