Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/f8a7188b0002a03257aae73ec4dab449 to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/f8a7188b0002a03257aae73ec4dab449 to your computer and use it in GitHub Desktop.
Custom configuration provider reading the settings from the database
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace CustomConfigProviderExample.CustomConfigProvider
{
public class CustomConfigurationProvider : ConfigurationProvider
{
public CustomConfigurationProvider(Action<DbContextOptionsBuilder> optionsAction)
{
OptionsAction = optionsAction;
}
Action<DbContextOptionsBuilder> OptionsAction { get; }
public override void Load()
{
var builder = new DbContextOptionsBuilder<CustomConfigurationProviderDbContext>();
OptionsAction(builder);
using var dbContext = new CustomConfigurationProviderDbContext(builder.Options);
dbContext.Database.EnsureCreated();
Data = !dbContext.ConfigurationSetting.Any()
? CreateAndSaveDefaultValues(dbContext)
: dbContext.ConfigurationSetting.ToDictionary(c => c.Key, c => c.Value);
}
private static IDictionary<string, string> CreateAndSaveDefaultValues(
CustomConfigurationProviderDbContext dbContext)
{
var configValues = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "Mode", "CustomConfigProvider" },
{ "MailFeature:0:Subject", "User account locked." },
{ "MailFeature:1:Subject", "Account Verification Required." }
};
dbContext.ConfigurationSetting
.AddRange(configValues.Select(kvp => new ConfigurationSetting
{
Key = kvp.Key,
Value = kvp.Value
})
.ToArray());
dbContext.SaveChanges();
return configValues;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment