-
Notifications
You must be signed in to change notification settings - Fork 13
Getting Started
Getting started couldn't be more easy.
Install the latest Nuget Package.
Create a Repository compatible with EntityFrameworkCore and the ConfigurationContext.
Add your settings to the repository.
Initialise Configuration.EntityFramework using the AddEntityFrameworkConfig method.
Default arguments
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json", true, true)
.AddEntityFrameworkConfig(builder => builder.UseSqlServer(@"Data Source=.;Initial Catalog=Configuration;Integrated Security=True"))
.Build();
Load settings for a specified Application
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json", true, true)
.AddEntityFrameworkConfig(builder => builder.UseSqlServer(@"Data Source=.;Initial Catalog=Configuration;Integrated Security=True", "PayrollWebAPI"))
.Build();
Filter settings using the Descriminator argument
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json", true, true)
.AddEntityFrameworkConfig(builder => builder.UseSqlServer(@"Data Source=.;Initial Catalog=Configuration;Integrated Security=True", @"{""Environment"":""Testing"", ""Username"":""Patrick""}"))
.Build();
Having completed steps 1 - 4, you are now ready to start accessing your settings.
The following snippets provide sample code for retrieving your settings via the IConfiguration instance which is returned by the ConfigurationBuilder.Build() method.
Check Configuration Section Exists.
var exists = config.SectionExists("SampleSection");
Get Configuration Section for complex type. Return null if section does not exist.
var section = config.TryGetSection<ComplexType>("SampleSection");
Get Configuration Section for complex type. Return default value if section does not exist.
var section = config.GetSection<ComplexType>("SampleSection");
Get Configuration Value for Key.
var setting = config.GetValue<string>("TestSetting");
The connection string can be configured via the DbContextOptionBuilder.
Alternatively, a ConnectionString can be added to the local config settings. In the case of an .xproj project, this would mean the appsettings.json file.
{
"ConnectionStrings": {
"ConfigurationContext": "Data Source=.;Initial Catalog=Configuration.Samples;Integrated Security=True"
}
}