0

I am trying to move my configuration values from my appsettings.json file into my SQL Server database. I have tried both this and this link but I am still getting a null reference error when it runs for OptionsAction either way. Please assist in any way possible and find my code below:

OrgConfigurationSource:

public class OrgConfigurationSource : IConfigurationSource
{
    public Action<DbContextOptionsBuilder> OptionsAction { get; set; }
    public bool ReloadOnChange { get; set; }
    public int ReloadDelay { get; set; } = 500;

    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return new OrgConfigurationProvider(this);
    }

OrgConfigurationProvider:

public class OrgConfigurationProvider : ConfigurationProvider
{
    private readonly OrgConfigurationSource source;

    public OrgConfigurationProvider(OrgConfigurationSource source)
    {
        this.source = source;
    }

    public override void Load()
    {
        var builder = new DbContextOptionsBuilder<StorageContext>();
        source.OptionsAction(builder);

        using (var context = new StorageContext(builder.Options))
        {
            context.Database.EnsureCreated();
            var config = context.ConfigurationValues.SingleOrDefault();
            if (config == null) return;

            Data = new Dictionary<string, string>();
            Data.Add($"{nameof(OrgConfigurationValue)}.{nameof(OrgConfigurationValue.Name)}", config.Name);
        }
    }

Program.cs:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureLogging( l => l.AddConsole())
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.AddOrgConfiguration();
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {             
            webBuilder.UseStartup<Startup>();
        });
}

EntityFrameworkExtensions:

public static class EntityFrameworkExtensions
{
    public static IConfigurationBuilder AddOrgConfiguration(this IConfigurationBuilder builder)
    {
        return builder.Add(new OrgConfigurationSource());
    }
}
madreflection
  • 4,744
  • 3
  • 19
  • 29
  • 1
    Formatting note: what comes after the three backticks is an optional language specifier. Because you were putting your first line of code there, it wasn't being rendered. – madreflection Sep 22 '20 at 18:47
  • 3
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Sep 22 '20 at 18:55
  • 1
    The event is never assigned in the code provided, just at the beginning: `public Action OptionsAction { get; set; }`. You need to write `source?.OptionsAction?.Invoke(builder);` –  Sep 22 '20 at 18:56
  • @OlivierRogier Thank you for that. I do know what a null reference exception is and that the object needs to be instantiated, but I am new to working with configuration so OptionsAction and DbContextBuilder is still a bit confusing for me. I did try that line and I am still unfortunately getting the same error. – Nicole Carrero Sep 22 '20 at 19:50
  • Do you write a Code or a Database First Entity Framework Model ? For the Code-First I recommend this small but effective book [Code-First Development with Entity Framework (Packt)](https://www.amazon.com/Code-First-Development-Entity-Framework-Barskiy/dp/1784396273/). I hope this can help you to enjoy C# coding: [How do I improve my knowledge in C#](http://www.ordisoftware.com/files/stack-overflow/CsharpBegin.htm). –  Sep 22 '20 at 19:54
  • @OlivierRogier I write code-first but from my research this process is what I found for using databases for configuration. I haven't found another way yet that makes more sense to me since I am still very new at configuration. – Nicole Carrero Sep 22 '20 at 20:05

0 Answers0