ConfigurationRepository.EntityFramework
1.1.1
dotnet add package ConfigurationRepository.EntityFramework --version 1.1.1
NuGet\Install-Package ConfigurationRepository.EntityFramework -Version 1.1.1
<PackageReference Include="ConfigurationRepository.EntityFramework" Version="1.1.1" />
<PackageVersion Include="ConfigurationRepository.EntityFramework" Version="1.1.1" />
<PackageReference Include="ConfigurationRepository.EntityFramework" />
paket add ConfigurationRepository.EntityFramework --version 1.1.1
#r "nuget: ConfigurationRepository.EntityFramework, 1.1.1"
#addin nuget:?package=ConfigurationRepository.EntityFramework&version=1.1.1
#tool nuget:?package=ConfigurationRepository.EntityFramework&version=1.1.1
ConfigurationRepository.EntityFramework
An ASP .NET Core class library for using databases as configuration repositories via Entity Framework ORM.
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 aValue
that contains parsable hierarchical structure of the configuration by thatKey
. Access to configuration storage is provided byEfParsableConfigurationRepository
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 usingAddEfCoreRepository()
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 withJsonConfigurationParser
usingAddEfCoreJsonRepository()
extension method and set theKey
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 | 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. 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. |
-
net8.0
- ConfigurationRepository (>= 1.1.1)
- Microsoft.EntityFrameworkCore (>= 8.0.14)
- Microsoft.EntityFrameworkCore.Relational (>= 8.0.14)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.