ConfiguredSqlConnection 1.2.0

dotnet add package ConfiguredSqlConnection --version 1.2.0                
NuGet\Install-Package ConfiguredSqlConnection -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ConfiguredSqlConnection" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ConfiguredSqlConnection --version 1.2.0                
#r "nuget: ConfiguredSqlConnection, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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. The dbName 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. The dbName 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
1.2.0 299 8/30/2024
1.1.5 2,140 12/8/2023
1.1.4 947 11/14/2023
1.1.3 1,275 10/17/2023
1.1.2 310 10/9/2023
1.1.1 1,198 6/16/2023
1.1.0 225 6/15/2023 1.1.0 is deprecated because it has critical bugs.
1.0.0 171 6/13/2023