From 02e637064e8dc5aa7f91ee2c8b1bdcdcce8ae8eb Mon Sep 17 00:00:00 2001 From: Stanislav Vysotskyi <66203238+ArdenHide@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:46:53 +0300 Subject: [PATCH] Update `DbContextOptionsBuilderExtensions` (#33) * - update `DbContextOptionsBuilderExtensions` * - cleanup - update `README` - update .csproj --- .../ConfiguredSqlConnection.csproj | 4 +-- .../DbContextOptionsBuilderExtensions.cs | 26 ++++++++++--------- README.md | 6 +++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/ConfiguredSqlConnection/ConfiguredSqlConnection.csproj b/ConfiguredSqlConnection/ConfiguredSqlConnection.csproj index 4b76b13..16d5c78 100644 --- a/ConfiguredSqlConnection/ConfiguredSqlConnection.csproj +++ b/ConfiguredSqlConnection/ConfiguredSqlConnection.csproj @@ -7,7 +7,7 @@ 5405e2de-7a05-4014-aea5-3ee845eb7fee True ConfiguredSqlConnection - 1.1.3 + 1.1.4 Senia,ArdenHide,Lomet The-Poolz https://www.nuget.org/packages/ConfiguredSqlConnection @@ -17,7 +17,7 @@ The NuGet package is a collection of utilities for working with SQL Server database connections using environment settings and secure connection strings. https://github.com/The-Poolz/ConfiguredSqlConnection LICENSE - - Update package `Utils.EnvironmentManager` to `2.0.1` + - Updated `ConfigureFromActionConnection`, added possible to pass assymbly of migrations project. diff --git a/ConfiguredSqlConnection/Extensions/DbContextOptionsBuilderExtensions.cs b/ConfiguredSqlConnection/Extensions/DbContextOptionsBuilderExtensions.cs index 6238b2c..a414a1e 100644 --- a/ConfiguredSqlConnection/Extensions/DbContextOptionsBuilderExtensions.cs +++ b/ConfiguredSqlConnection/Extensions/DbContextOptionsBuilderExtensions.cs @@ -1,6 +1,7 @@ using SecretsManager; using EnvironmentManager; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; namespace ConfiguredSqlConnection.Extensions; @@ -8,30 +9,31 @@ 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("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("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? ConfigureSqlServerOptionsAction(string? migrationsAssembly = null) => + !string.IsNullOrWhiteSpace(migrationsAssembly) + ? options => options.MigrationsAssembly(migrationsAssembly) + : null; } diff --git a/README.md b/README.md index 14e503d..b2d2abf 100644 --- a/README.md +++ b/README.md @@ -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"); ```