Created
April 18, 2021 13:37
-
-
Save manoj-choudhari-git/f8a7188b0002a03257aae73ec4dab449 to your computer and use it in GitHub Desktop.
Custom configuration provider reading the settings from the database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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