ConfiguredSqlConnection 1.1.3
See the version list below for details.
dotnet add package ConfiguredSqlConnection --version 1.1.3
NuGet\Install-Package ConfiguredSqlConnection -Version 1.1.3
<PackageReference Include="ConfiguredSqlConnection" Version="1.1.3" />
paket add ConfiguredSqlConnection --version 1.1.3
#r "nuget: ConfiguredSqlConnection, 1.1.3"
// Install ConfiguredSqlConnection as a Cake Addin #addin nuget:?package=ConfiguredSqlConnection&version=1.1.3 // Install ConfiguredSqlConnection as a Cake Tool #tool nuget:?package=ConfiguredSqlConnection&version=1.1.3
ConfiguredSqlConnection
The NuGet package is a collection of utilities for working with SQL Server database connections using environment settings and secure connection strings. It includes classes such as EnvManager
, ConnectionOptionsBuilder
, and SecretManager
that facilitate retrieving and configuring connection parameters based on environment variables and secure secrets. This package aims to simplify the process of configuring and utilizing SQL Server connections in your application.
Creating a TContext using DbContextFactory<TContext>
Create a TContext
instance based on the desired context option:
var dbContext = new DbContextFactory<TContext>().Create(ContextOption.Prod, dbName);
The following context options are available:
public enum ContextOption
{
Prod,
Staging,
InMemory
}
ContextOption.Prod:
Retrieves the connection string from the environment variable "CONFIGUREDSQLCONNECTION_SECRET_NAME_OF_CONNECTION". This option is suitable for production environments.ContextOption.Staging:
Reads the connection string from the "appsettings.json" file. ThedbName
parameter can be provided to specify the target database name. This option is useful for staging or development environments.ContextOption.InMemory:
Uses an in-memory database for testing or development purposes. ThedbName
parameter can be provided to set a custom database name.dbName
parameter is optional and should be provided only for the "Staging" or "InMemory" options. It is not required for the "Prod" option.
Creating a TContext using DbContextEnvironmentFactory<TContext>
If you want to switch modes using environment variables, you can use the DbContextEnvironmentFactory<TContext>
class.
It is derived from the DbContextFactory<TContext>
and provides additional functionality to retrieve the mode and database name from environment variables.
var dbContext = new DbContextEnvironmentFactory().CreateFromEnvironment();
The CreateFromEnvironment()
method retrieves the mode and database name from the environment variables CONFIGUREDSQLCONNECTION_DB_MODE
and CONFIGUREDSQLCONNECTION_DB_NAME
, respectively.
Environment variables
CONFIGUREDSQLCONNECTION_DB_MODE
: environment variable should contain one of the following values: "Prod", "Staging", or "InMemory", which determine the desired context option.CONFIGUREDSQLCONNECTION_DB_NAME
: environment variable can be used to specify the database name for the "Staging" or "InMemory" options.CONFIGUREDSQLCONNECTION_SECRET_NAME_OF_CONNECTION
: environment variable should contain the connection string for the "Prod" option.CONFIGUREDSQLCONNECTION_ACTION_CONNECTION
: environment variable should contain the connection string. This is required for GitHub Actions to obtain the database connection string, enabling it to execute the migrations. Ensure that your connection string is correctly formatted and points to the appropriate database, as all migrations will be applied there.
"appsettings.json" file
Ensure that your appsettings.json
file is located in the root folder of your project. This file should contain the necessary configuration settings, including the connection strings for different contexts.
{
"ConnectionStrings": {
"LocalTestDb": "Server=(localdb)\\MyTestDb;Initial Catalog=TestDb",
"ProdDb": "Data Source=maindatabase.com,1234;User id=testUser;Password=password;Initial Catalog=ProdDb;TrustServerCertificate=true;",
"StagingDb": "Data Source=maindatabase.com,1234;User id=testUser;Password=password;Initial Catalog=StagingDb;TrustServerCertificate=true;"
}
}
Creating a customized DbContextOptionsBuilder<TContext>
This class is a factory for creating DbContextOptionsBuilder<TContext>
instances based on the provided ContextOption
and optional database name (dbName
).
var optionsBuilder = new DbContextOptionsBuilderFactory<TContext>().Create(ContextOption.YourOption, dbName)
ConnectionStringFactory
This static class provides methods to retrieve connection strings based on the provided ContextOption
and optional dbName
.
var connectionString = ConnectionStringFactory.GetConnection(ContextOption.YourOption, dbName)
ConnectionStringFromEnvironmentFactory
This static class provides a method to retrieve a connection string from environment variables (CONFIGUREDSQLCONNECTION_DB_MODE
and CONFIGUREDSQLCONNECTION_DB_NAME
).
var connectionString = ConnectionStringFactory.GetConnectionFromEnvironment()
DbContextOptionsBuilderExtensions
This static class provides an extensions methods to configure a DbContextOptionsBuilder
instance in method OnConfiguring
from DbContext
.
Each these method do nothing if DbContextOptionsBuilder
already be configured.
// Try to configure action connection.
optionsBuilder
.ConfigureFromActionConnection();
// Try to configure db connection via SecretManager.
optionsBuilder
.ConfigureFromSecretConnection();
// Try to configure action connection also try to configure db connection via SecretManager.
optionsBuilder
.ConfigureFromActionConnection()
.ConfigureFromSecretConnection();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.EntityFrameworkCore.InMemory (>= 7.0.5)
- Microsoft.EntityFrameworkCore.SqlServer (>= 7.0.5)
- Microsoft.Extensions.Configuration.UserSecrets (>= 6.0.1)
- SecretsManager (>= 1.0.0)
- TestableDbContext.Mock (>= 1.0.0)
- Utils.EnvironmentManager (>= 2.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ConfiguredSqlConnection:
Package | Downloads |
---|---|
DownloaderV3.DataBase
DownloaderV3 DataBase |
GitHub repositories
This package is not used by any popular GitHub repositories.
- Update package `Utils.EnvironmentManager` to `2.0.1`