Skip to content

Commit

Permalink
Update DbContextOptionsBuilderExtensions (#33)
Browse files Browse the repository at this point in the history
* - update `DbContextOptionsBuilderExtensions`

* - cleanup
- update `README`
- update .csproj
  • Loading branch information
ArdenHide authored Nov 14, 2023
1 parent b53058d commit 02e6370
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
4 changes: 2 additions & 2 deletions ConfiguredSqlConnection/ConfiguredSqlConnection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<UserSecretsId>5405e2de-7a05-4014-aea5-3ee845eb7fee</UserSecretsId>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>ConfiguredSqlConnection</Title>
<Version>1.1.3</Version>
<Version>1.1.4</Version>
<Authors>Senia,ArdenHide,Lomet</Authors>
<Company>The-Poolz</Company>
<PackageProjectUrl>https://www.nuget.org/packages/ConfiguredSqlConnection</PackageProjectUrl>
Expand All @@ -17,7 +17,7 @@
<Description>The NuGet package is a collection of utilities for working with SQL Server database connections using environment settings and secure connection strings.</Description>
<RepositoryUrl>https://github.com/The-Poolz/ConfiguredSqlConnection</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReleaseNotes>- Update package `Utils.EnvironmentManager` to `2.0.1`</PackageReleaseNotes>
<PackageReleaseNotes>- Updated `ConfigureFromActionConnection`, added possible to pass assymbly of migrations project.</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
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;
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,10 @@ optionsBuilder
optionsBuilder
.ConfigureFromActionConnection()
.ConfigureFromSecretConnection();

// Try to configure action connection, with migration assembly
// also try to configure db connection via SecretManager with migration assembly.
optionsBuilder
.ConfigureFromActionConnection("MyProject.Migrations")
.ConfigureFromSecretConnection("MyProject.Migrations");
```

0 comments on commit 02e6370

Please sign in to comment.