-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
DbContextOptionsBuilderExtensions
(#33)
* - update `DbContextOptionsBuilderExtensions` * - cleanup - update `README` - update .csproj
- Loading branch information
Showing
3 changed files
with
22 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 14 additions & 12 deletions
26
ConfiguredSqlConnection/Extensions/DbContextOptionsBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
using SecretsManager; | ||
using EnvironmentManager; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
|
||
namespace ConfiguredSqlConnection.Extensions; | ||
|
||
public static class DbContextOptionsBuilderExtensions | ||
{ | ||
private static readonly EnvManager envManager = new(); | ||
|
||
public static DbContextOptionsBuilder ConfigureFromActionConnection(this DbContextOptionsBuilder optionsBuilder) | ||
public static DbContextOptionsBuilder ConfigureFromActionConnection(this DbContextOptionsBuilder optionsBuilder, string? migrationsAssembly = null) | ||
{ | ||
if (optionsBuilder.IsConfigured) return optionsBuilder; | ||
|
||
var connectionString = envManager.GetEnvironmentValue<string>("CONFIGUREDSQLCONNECTION_ACTION_CONNECTION"); | ||
if (!string.IsNullOrEmpty(connectionString)) | ||
{ | ||
optionsBuilder.UseSqlServer(connectionString); | ||
} | ||
if (string.IsNullOrEmpty(connectionString)) return optionsBuilder; | ||
|
||
optionsBuilder.UseSqlServer(connectionString, ConfigureSqlServerOptionsAction(migrationsAssembly)); | ||
|
||
return optionsBuilder; | ||
} | ||
|
||
public static DbContextOptionsBuilder ConfigureFromSecretConnection(this DbContextOptionsBuilder optionsBuilder) | ||
public static DbContextOptionsBuilder ConfigureFromSecretConnection(this DbContextOptionsBuilder optionsBuilder, string? migrationsAssembly = null) | ||
{ | ||
if (optionsBuilder.IsConfigured) return optionsBuilder; | ||
|
||
var secretValue = envManager.GetEnvironmentValue<string>("CONFIGUREDSQLCONNECTION_SECRET_NAME_OF_CONNECTION", true); | ||
var connectionString = new SecretManager().GetSecretValue(secretValue, "connectionString"); | ||
if (!string.IsNullOrEmpty(connectionString)) | ||
{ | ||
optionsBuilder.UseSqlServer(connectionString); | ||
} | ||
if (string.IsNullOrEmpty(connectionString)) return optionsBuilder; | ||
|
||
optionsBuilder.UseSqlServer(connectionString, ConfigureSqlServerOptionsAction(migrationsAssembly)); | ||
|
||
return optionsBuilder; | ||
} | ||
|
||
private static Action<SqlServerDbContextOptionsBuilder>? ConfigureSqlServerOptionsAction(string? migrationsAssembly = null) => | ||
!string.IsNullOrWhiteSpace(migrationsAssembly) | ||
? options => options.MigrationsAssembly(migrationsAssembly) | ||
: null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters