Diamond.Configuration 2026.0.0

dotnet add package Diamond.Configuration --version 2026.0.0
                    
NuGet\Install-Package Diamond.Configuration -Version 2026.0.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="Diamond.Configuration" Version="2026.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Diamond.Configuration" Version="2026.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Diamond.Configuration" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Diamond.Configuration --version 2026.0.0
                    
#r "nuget: Diamond.Configuration, 2026.0.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.
#:package Diamond.Configuration@2026.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Diamond.Configuration&version=2026.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Diamond.Configuration&version=2026.0.0
                    
Install as a Cake Tool

Diamond.Configuration

Diamond.Configuration adds a database-backed provider to .NET configuration. It loads key/value pairs from a database into IConfiguration, so application code can continue to use the normal configuration APIs such as configuration["Services:SMSClient:Host"] and GetSection(...).

Install

dotnet add package Diamond.Configuration

Add the ADO.NET provider package for your database separately. For example, with SQL Server:

dotnet add package Microsoft.Data.SqlClient

Database layout

By default, the provider expects a table named Configurations with these columns:

Column Purpose
App Optional application identifier; NULL is used when AppId is not set.
ID Configuration key, such as Services:SMSClient:Host.
Value Configuration value stored as text.

Create a table with compatible column types and a unique constraint/index on (App, ID) (or on ID when application scoping is disabled). Exact SQL types and identifier quoting depend on the database engine. The library does not create or migrate the table.

For SQL Server, a table matching the default column names can be created like this:

CREATE TABLE [Configurations] --Registry
(
    [App]   varchar(50)  NOT NULL,
    [Id]    varchar(200) NOT NULL,
    [Value] nvarchar(4000)   NULL,
    CONSTRAINT [PK_Configurations] PRIMARY KEY CLUSTERED ([App] ASC, [Id] ASC)
);

If you disable application scoping by setting AppColumnName to null or an empty string, use a unique constraint on ID alone instead.

Configure and load

The provider is lazy: AddDbConfiguration adds its source, and UseDbConfiguration starts it and loads values from the database. Here is a standalone example using SQL Server:

using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;

builder.AddDbConfiguration(options =>
{
    options.AppId = "API-PROD";
});

string connectionString = builder.Configuration.GetConnectionString("default")!; //read from appsettings.json
builder.Configuration.UseDbConfiguration(options => options.SetConnection<Microsoft.Data.SqlClient.SqlConnection>(connectionString));
//builder.Configuration.UseDbConfiguration(options => options.SetConnection<Oracle.ManagedDataAccess.Client.OracleConnection>(connectionString));
//builder.Configuration.UseDbConfiguration(options => options.SetConnection<Npgsql.NpgsqlConnection>(connectionString));
//builder.Configuration.UseDbConfiguration(options => options.SetConnection<MySql.Data.MySqlClient.MySqlConnection>(connectionString));
//builder.Configuration.UseDbConfiguration(options => options.SetConnection<Microsoft.Data.Sqlite.SqliteConnection>(connectionString));

string? host = builder.Configuration["Services:SMSClient:Host"];

...

//inject
public HomeController(IConfiguration configuration)
{
	string? host = configuration["Services:SMSClient:Host"];
}

OR

using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;

var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", optional: true)
    .AddEnvironmentVariables()
    .AddDbConfiguration(options =>
    {
        options.AppId = "API-PROD";
    })
    .Build();

string connectionString = configuration.GetConnectionString("default"); //read from appsettings.json
configuration.UseDbConfiguration(options => options.SetConnection<SqlConnection>(connectionString));

string? host = configuration["Services:SMSClient:Host"];

Configuration providers are applied in registration order, and later providers take precedence for duplicate keys. In this example, database values override JSON and environment values with the same key. Register the database source after providers that should have lower precedence.

For a different database, pass its DbConnection type to SetConnection<TConnection>. This method chooses a parameter prefix for common SQL Server, Oracle, MySQL/MariaDB, PostgreSQL, and SQLite connection types. For an unrecognized provider, set DbConnection, ConnectionString, and ParamSymbol explicitly in the options callback.

Options

DbConfigurationOptions supports these main settings:

Option Default Description
ConnectionString unset Database connection string.
DbConnection unset Concrete DbConnection type with a public parameterless constructor. SetConnection<TConnection> sets this and the connection string.
SchemaName unset Optional schema prefix for the table.
TableName Configurations Configuration table name.
AppColumnName App Application-id column. Set to null or empty to disable application scoping.
AppId unset Identifies the application configuration set to load; when omitted and AppColumnName is enabled, rows with a NULL application id are used.
KeyColumnName ID Configuration key column.
ValueColumnName Value Configuration value column.
ParamSymbol provider-dependent SQL parameter prefix; inferred by SetConnection<TConnection> for recognized providers.
CommandTimeout 0 Command timeout in seconds; zero leaves the provider default.
WatchdogPeriod 30 seconds Polling interval used to check the timestamp key. Values below one second are treated as unset and the watchdog uses its fallback interval.
TimestampId Monitor:Timestamp Internal key used to detect database changes.
WatchdogPeriodId Monitor:WatchdogPeriod Optional database key that can override the polling interval.

The watchdog detects changes when the timestamp value changes. When writing through the repository, the library updates this timestamp automatically. If another process edits configuration rows directly, that process should also update the configured timestamp key for other application instances to reload promptly.

Sharing a database across applications and environments

AppId lets multiple applications or deployment environments keep separate configuration sets in the same database table. Rows are selected by the configured application id, so give each distinct application/environment combination its own id—for example, OrdersApi:Development, OrdersApi:Production, or BillingApi:Production. This avoids mixing settings even when different sets use the same configuration keys.

You can choose the id from any configuration source available during startup, such as an environment-specific appsettings.json file or an environment variable, and pass it to DbConfigurationOptions. This makes it possible to switch which set is loaded by changing the selected AppId for that deployment. If AppId is not set, the provider selects rows whose App column is NULL; set AppColumnName to null or empty to disable application scoping entirely.

Protecting keys and values

You can use ASP.NET Core Data Protection to protect keys and/or values stored in the table. Configure the data protection services in the host, create a purpose-specific IDataProtector, and pass it through SetKeyProtector or SetValueProtector:

using Microsoft.AspNetCore.DataProtection;

var protector = dataProtectionProvider.CreateProtector("Diamond.Configuration.Values");
options.SetValueProtector(protector);

Use the same compatible data protection key ring and purpose across application instances that share the database. Protecting configuration values does not encrypt the connection string or database itself. Keep secrets such as connection strings in a secure configuration source.

Notes

  • Values are stored as strings, matching the standard IConfiguration model. Bind sections to options with the usual .NET configuration binding APIs.
  • Table and column names are inserted into SQL statements as identifiers. Configure them as trusted schema values; SQL parameters protect the values, not identifiers.
  • UseDbConfiguration should be called once for each configuration root after the host services needed by its options are available. The first database load occurs when it is called.

Sample

Samples: https://github.com/Hajisharifi/Diamond-Samples/tree/master/DbConfiguration

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  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 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2026.0.0 54 10/31/2026
2024.0.0 301 11/8/2024