Skip to content

Add settings to the Repository

Patrick edited this page Nov 2, 2016 · 4 revisions

The repository can be populated via a script, database tooling or even the EntityFrameworkCore ConfigurationContext.

Categorization of Configuration Data

Before inserting data, it is important that you understand how to categorize your data.

To Add Settings Programatically using the ConfigurationContext

Simple AppSetting

using (var context = new ConfigurationContext())
{
    var section = new SectionEntity() { ApplicationName = "Sample", SectionName = "appSettings", Aspect = "settings" };
    context.Sections.Add(section);
    context.SaveChanges();

    var setting = new SettingEntity() { SectionId = section.Id, Key = "TestSetting", Json = @"""Test Value""" };
    context.Settings.Add(setting);
    context.SaveChanges();
}

ComplexType

using (var context = new ConfigurationContext())
{
    var complex = new ComplexType()
    {
        Id = Guid.NewGuid(),
        Name = "ComplexTypeName"
    };

    var section = new SectionEntity() { ApplicationName = "SampleApplication", SectionName = "SampleComplexTypeSection" };
    context.Sections.Add(section);
    context.SaveChanges();

    var setting = new SettingEntity() { SectionId = section.Id, Key = "SampleComplexType" };
    setting.SetValue(complex);

    context.Settings.Add(setting);
    context.SaveChanges();
}