ConfigurationRepository.EntityFramework 1.1.1

dotnet add package ConfigurationRepository.EntityFramework --version 1.1.1
                    
NuGet\Install-Package ConfigurationRepository.EntityFramework -Version 1.1.1
                    
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="ConfigurationRepository.EntityFramework" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ConfigurationRepository.EntityFramework" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="ConfigurationRepository.EntityFramework" />
                    
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 ConfigurationRepository.EntityFramework --version 1.1.1
                    
#r "nuget: ConfigurationRepository.EntityFramework, 1.1.1"
                    
#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.
#addin nuget:?package=ConfigurationRepository.EntityFramework&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=ConfigurationRepository.EntityFramework&version=1.1.1
                    
Install as a Cake Tool

ConfigurationRepository.EntityFramework

An ASP .NET Core class library for using databases as configuration repositories via Entity Framework ORM.

NuGet NuGet

Installation:

  • from NuGet;
  • from package manager console:
Install-Package ConfigurationRepository.EntityFramework
  • from command line:
dotnet add package ConfigurationRepository.EntityFramework

Usage:

The configuration can be stored in two different structured forms, we have to choose one:

  • A table of single configuration with it`s keys and values, like in a dictionary. Access to configuration storage is provided by EfDictionaryConfigurationRepository class.
  • A table of multiple configurations, one of which is extracted using the Key and a Value that contains parsable hierarchical structure of the configuration by that Key. Access to configuration storage is provided by EfParsableConfigurationRepository class.

Currently, the only format supported for EfParsableConfigurationRepository is in JSON format. This can be easily extended implementing IConfigurationParser interface for any format needed.

Registration of EfCore configuration provider with EfDictionaryConfigurationRepository for ConfigurationBuilder:

using ConfigurationRepository.EntityFramework;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

var config = new ConfigurationBuilder()
    .AddEfCoreRepository(options => options
        .UseInMemoryDatabase("ConfigurationDatabase")
        .UseTable("ConfigurationTable"))
    .Build();

Here we:

  • Create configuration builder ConfigurationBuilder;
  • Register EfCore dictionary repository EfDictionaryConfigurationRepository for this builder as configuration provider using AddEfCoreRepository() extension method;
  • Configure database context options:
    • For simplicity we are using in-memory database with UseInMemoryDatabase() extension method. This can be replaced by any database supported by Entity Framework.
    • With UseTable() extension method we set the name for our configuration table.
  • Build configuration.

Registration of EfCore configuration provider with EfParsableConfigurationRepository (JSON) for ConfigurationBuilder:

using ConfigurationRepository.EntityFramework;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

var config = new ConfigurationBuilder()
    .AddEfCoreJsonRepository("ConfigurationKey", options => options
        .UseInMemoryDatabase("ConfigurationDatabase")
        .UseTable("ConfigurationTable"))
    .Build();

Here we:

  • Create configuration builder ConfigurationBuilder;
  • Register EfCore parsable repository EfParsableConfigurationRepository for this builder as configuration provider with JsonConfigurationParser using AddEfCoreJsonRepository() extension method and set the Key to concrete configuration;
  • Configure database context options:
    • For simplicity we are using in-memory database with UseInMemoryDatabase() extension method. This can be replaced by any database supported by Entity Framework.
    • Using UseTable() extension method we set the name for our configuration table.
  • Build configuration.

Examples:

An example that demonstrates how we can create database context and use it to add configuration to a database and also access it through configuration service. This one is base on EfDictionaryConfigurationRepository:

using ConfigurationRepository;
using ConfigurationRepository.EntityFramework;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

// Create configuration databae context using in-memory database for simplicity.
var options = ConfigurationDbContextOptionsFactory.Create(
    options => options.UseInMemoryDatabase("TestDb"));

// Create configuration context using these options.
await using var configurationContext = new ConfigurationRepositoryDbContext(options);

// Add a key-value pair to configuration table.
configurationContext.ConfigurationEntryDbSet.Add(new ConfigurationEntry("Key", "value"));
await configurationContext.SaveChangesAsync();

// Create configuration using Entity Framework repository with created database context options.
var config = new ConfigurationBuilder()
    .AddEfCoreRepository(options)
    .Build();

// Get a value from the configuration.
Console.WriteLine(config["key"]);

And in case we intend to use EfParsableConfigurationRepository:

using ConfigurationRepository;
using ConfigurationRepository.EntityFramework;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

// Create configuration databae context using in-memory database for simplicity.
var options = ConfigurationDbContextOptionsFactory.Create(
    options => options.UseInMemoryDatabase("TestDb"));

// Create configuration context using these options.
await using var configurationContext = new ConfigurationRepositoryDbContext(options);

// Add a configuration to configurations table with the Key = "DatabaseConfig".
configurationContext.ConfigurationEntryDbSet.Add(new ConfigurationEntry("DatabaseConfig", """
    {
      "ConnectionStrings": {
        "Host": "A connection string to a host"
      }
    }
    """));
await configurationContext.SaveChangesAsync();

// Create configuration using Entity Framework json repository with created database context options.
var config = new ConfigurationBuilder()
    .AddEfCoreJsonRepository("DatabaseConfig", options)
    .Build();

// Get the Value = "A connection string to a host" from the configuration.
Console.WriteLine(config.GetConnectionString("Host"));

This example you may find in ConfigurationRepository.EfCore sample project on github.

If our database source can change in time we may also add configuration reloader that will periodically reload our configuration from database:

using ConfigurationRepository;
using ConfigurationRepository.EntityFramework;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddEfCoreRepository(
    options => options
        .UseInMemoryDatabase("ConfigurationDatabase")
        .UseTable("ConfigurationTable"),
    source => source.WithPeriodicalReload());

builder.Services.AddConfigurationReloader();

var app = builder.Build();

app.Run();
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.  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 was computed.  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. 
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
1.1.1 117 7/1/2025
1.1.0 108 6/29/2025
1.0.0 134 6/16/2025