ConfigurationLibrary 1.0.6

dotnet add package ConfigurationLibrary --version 1.0.6                
NuGet\Install-Package ConfigurationLibrary -Version 1.0.6                
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="ConfigurationLibrary" Version="1.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ConfigurationLibrary --version 1.0.6                
#r "nuget: ConfigurationLibrary, 1.0.6"                
#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 ConfigurationLibrary as a Cake Addin
#addin nuget:?package=ConfigurationLibrary&version=1.0.6

// Install ConfigurationLibrary as a Cake Tool
#tool nuget:?package=ConfigurationLibrary&version=1.0.6                

Connection helpers for data providers

Configuration

This library provides a method to obtain a connection string to use with a data provider or Entity Framework Core, intended for desktop applications as ASP.NET Core has this already with more options via dependency injection.

In the configuration file, ActiveEnvironment points to the environment to connect, development, stage (test) and production.

Well someone may say I only have one database server. Then set the environment to any of the environments than if at a later time more servers are available you are ready.

11-19-2023 changed to release mode


Simple example for obtaining connection strings.

Get current connection by ActiveEnvironment

ConnectionString()

appsettings.json

{
  "ConnectionsConfiguration": {
    "ActiveEnvironment": "Production",
    "Development": "Dev connection string goes here",
    "Stage": "Stage connection string goes here",
    "Production": "Prod connection string goes here"
  }
}

Above, ConnectionString() will be for Production

Used with a data provider

public class DataProviderOperations
{
    public static async Task<bool> CanConnect()
    {
        await using var cn = new SqlConnection(ConfigurationHelper.ConnectionString());
        await using var cmd = new SqlCommand(){Connection = cn};
        cmd.CommandText = "SELECT ContactId, FirstName, LastName FROM dbo.Contact1;";
        try
        {
            await cn.OpenAsync();

            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}

Used with Entity Framework Core

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        optionsBuilder.UseSqlServer(ConfigurationHelper.ConnectionString());
    }
}

Basic usage

Set connection strings in appsettings.json and copy appsettings.json to the application folder followed by using ConfigurationHelper.ConnectionString() to set a connection string.

Inspired by

An earlier repository I wrote on connection to databases.

Working with multiple environments for connection strings

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
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 ConfigurationLibrary:

Package Downloads
EntityFrameworkCoreHelpers

Provides methods for connecting to SQL-Server databases for EF Core 5.x

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 2,147 11/22/2023
1.0.5 446 11/19/2023
1.0.4 1,501 12/14/2022
1.0.2 618 10/8/2022
1.0.1 1,999 4/23/2022

Added methods to ASP.NET Core