You've already forked Extensions.Configuration.EntityFrameworkCore
Working implementation
This commit is contained in:
56
RAIC.Extensions.Configuration.EntityFrameworkCore/README.md
Normal file
56
RAIC.Extensions.Configuration.EntityFrameworkCore/README.md
Normal file
@@ -0,0 +1,56 @@
|
||||
# RAIC.Extensions.Configuration.EntityFrameworkCore
|
||||
|
||||
This library is a `Microsoft.Extensions.Configuration.IConfigurationProvider` that reads settings from a `DbSet<ISetting> Settings` property
|
||||
present on your Entity Framework Core `DbContext`.
|
||||
|
||||
## Goals
|
||||
1. Usable with minimal constraints on your entity model
|
||||
1. Follows conventional configuration patterns
|
||||
1. `IOptionsMonitor` update support through optional opt-in services (see `RAIC.Extensions.Configuration.EntityFrameworkCore.PostgreSQL` & `RAIC.Extensions.Configuration.EntityFrameworkCore.SqlServer`)
|
||||
|
||||
|
||||
## Requirements
|
||||
* .NET 8
|
||||
|
||||
|
||||
## Gotchas
|
||||
* Setting values cannot be `null` (as signified by the `RequiredAttribute` on `ISetting.Value`)
|
||||
* Setting keys should not contain the `=` character (similar to `CommandLineConfigurationProvider` & `EnvironmentVariablesConfigurationProvider`)
|
||||
|
||||
|
||||
## Usage Example
|
||||
|
||||
```csharp
|
||||
|
||||
using RAIC.Extensions.Configuration.EntityFrameworkCore.Extensions;
|
||||
|
||||
public record Setting : ISetting
|
||||
{
|
||||
[Key]
|
||||
public required string Key { get; set; }
|
||||
|
||||
[Required]
|
||||
public required string Value { get; set; }
|
||||
}
|
||||
|
||||
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>, Setting>
|
||||
{
|
||||
public DbSet<Setting> Settings { get; set; }
|
||||
}
|
||||
|
||||
var builder = Host.CreateApplicationBuilder(args) // or WebApplication.CreateBuilder(args);
|
||||
|
||||
// build an initial configuration
|
||||
builder.Configuration.AddJsonFile("appsettings.json")
|
||||
.AddUserSecrets<Program>(); // or whereever your connection string lives
|
||||
|
||||
// obtain connection string from preliminary config so can initialise other settings from DbSet
|
||||
builder.Configuration.AddDbSet<MyDbContext, Setting>(dbContextOptions => dbContextOptions.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
|
||||
|
||||
...
|
||||
|
||||
await builder.Build().RunAsync(); // use config as normal
|
||||
|
||||
```
|
||||
|
||||
Read more about [Configuration](https://docs.microsoft.com/en-us/dotnet/core/extensions/configuration) on the Microsoft Docs site.
|
||||
Reference in New Issue
Block a user