ConfiguredSqlConnection 1.2.0
dotnet add package ConfiguredSqlConnection --version 1.2.0
NuGet\Install-Package ConfiguredSqlConnection -Version 1.2.0
<PackageReference Include="ConfiguredSqlConnection" Version="1.2.0" />
paket add ConfiguredSqlConnection --version 1.2.0
#r "nuget: ConfiguredSqlConnection, 1.2.0"
// Install ConfiguredSqlConnection as a Cake Addin #addin nuget:?package=ConfiguredSqlConnection&version=1.2.0 // Install ConfiguredSqlConnection as a Cake Tool #tool nuget:?package=ConfiguredSqlConnection&version=1.2.0
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();
// 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");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.EntityFrameworkCore.InMemory (>= 8.0.8)
- Microsoft.EntityFrameworkCore.SqlServer (>= 8.0.8)
- Microsoft.Extensions.Configuration.UserSecrets (>= 8.0.0)
- SecretsManager (>= 1.0.0)
- TestableDbContext.Mock (>= 1.0.0)
- Utils.EnvironmentManager (>= 4.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.