???????????????????????????????????????ConfigurationProvider????????????????????????????????????????е?????????????????????????????ConfigurationProvider?????????????????????????????????????????????????洢????????????????????????????????????????????????????????ConfigurationProvider?????????μ?Entity Framework 7???????????????????
??????
????MemoryConfigurationProvider
????EnvironmentVariablesConfigurationProvider
????CommandLineConfigurationProvider
????JsonConfigurationProvider
????XmlConfiguationProvider
????IniConfigurationProvider
?????????ConfigurationProvider
?????????????????ConfigurationProvider?????DbConfigurationProvider???????????????????????????????????????????????е???á???????????ASP.NET Core???????????????????DbConfigurationProvider???????????????????Entity Framework 7?????????SQL Server??????????????????project.json????а??????μ???????“EntityFramework.MicrosoftSqlServer”???NuGet??????????
1: {
2:   ...
3:   "dependencies": {
4:     "Microsoft.Extensions.Configuration": "1.0.0-rc1-final"??
5:     "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final"
6:   }??
7: }
??????????????μ????????????ò?????????Profile????????????????????????AddDatabase???????DbConfigurationProvider?????????????????ConfigurationBuilder???????????????????????AddDatabase????????????????????????????????Щ?????????????????????????????????????У??????????????????????????????????????????Profile?????????????
1: string connectionString = "...";
2: Profile profile = new ConfigurationBuilder().AddDatabase(optionsBuilder => optionsBuilder.UseSqlServer(connectionString)??
3:         new Dictionary<string?? string>
4:         {
5:             ["Profile:Gender"]                    = "Male"??
6:             ["Profile:Age"]                       = "18"??
7:             ["Profile:ContactInfo:Email"]         = "foobar@outlook.com"??
8:             ["Profile:ContactInfo:PhoneNo"]       = "123456789"
9:         })
10:     .Build().Get<Profile>("Profile");
???????????????????????????????DbConfigurationProvider????y??????????????ConfigurationBuilder????????????AddDatabase????????????????????????????????????????????????????????????DbConfigurationProvider????Entity Framework 7??Code First??????????????????????????ApplicationSetting???????????????POCO?????????????????Key??Сд?????洢???????ApplicationSettingsContext??????DbContext?????
1: [Table("ApplicationSettings")]
2: public class ApplicationSetting
3: {
4:     private string key;
5:
6:     [Key]
7:     public string Key
8:     {
9:         get { return key; }
10:         set { key = value.ToLowerInvariant(); }
11:     }
12:
13:     [Required]
14:     [MaxLength(512)]
15:     public string Value { get; set; }
16:
17:     public ApplicationSetting()
18:     {}
19:
20:     public ApplicationSetting(string key?? string value)
21:     {
22:         this.Key     = key;
23:         this.Value     = value;
24:     }
25: }
26:
27: public class ApplicationSettingsContext : DbContext
28: {
29:     public ApplicationSettingsContext(DbContextOptions options) : base(options)
30:     {}
31:
32:     public DbSet<ApplicationSetting> Settings { get; set; }
33: }
???????????????DbConfigurationProvider?????????AddDatabase????塣DbConfigurationProvider?????????????????????????????????????Action<DbContextOptionsBuilder>???????????DbContext?????DbContextOptions??????????????????????????????Щ??????????????????????д??Load?????У??????????????DbContexts????????ж?????е??????????????????????
1: public class DbConfigurationProvider: ConfigurationProvider
2: {
3:     public Func<DbContextOptions> DbContextOptionsAccessor { get; private set; }
4:
5:     public DbConfigurationProvider(Action<DbContextOptionsBuilder> setup?? IEnumerable<KeyValuePair<string?? string>> settings = null)
6:     {
7:         DbContextOptionsBuilder<ApplicationSettingsContext> optionsBuilder = new DbContextOptionsBuilder<ApplicationSettingsContext>();
8:         setup(optionsBuilder);
9:         this.DbContextOptionsAccessor = () => optionsBuilder.Options;
10:
11:         if (settings!=null && settings.Any())
12:         {
13:             using (ApplicationSettingsContext dbContext = new ApplicationSettingsContext(this.DbContextOptionsAccessor()))
14:             {
15:                 dbContext.Database.EnsureCreated();
16:                 foreach (var item in settings)
17:                 {
18:                     ApplicationSetting setting = dbContext.Settings.FirstOrDefault(it => it.Key == item.Key.ToLowerInvariant());
19:                     if (null == setting)
20:                     {
21:                         dbContext.Settings.Add(new ApplicationSetting(item.Key?? item.Value));
22:                     }
23:                     else
24:                     {
25:                         setting.Value = item.Value;
26:                     }
27:                 }
28:                 dbContext.SaveChanges();
29:             }
30:         }
31:     }
32:
33:     public override void Load()
34:     {
35:         using (ApplicationSettingsContext dbContext = new ApplicationSettingsContext(this.DbContextOptionsAccessor()))
36:         {
37:             var dictionary = dbContext.Settings.ToDictionary(it => it.Key?? it => it.Value);
38:             this.Data = new Dictionary<string?? string>(dictionary?? StringComparer.OrdinalIgnoreCase);
39:         }
40:     }
41: }
42:
43: public static class DbConfigurationProviderExtensions
44: {
45:     public static IConfigurationBuilder AddDatabase(this IConfigurationBuilder builder?? Action<DbContextOptionsBuilder> setup??
46:         IEnumerable<KeyValuePair<string?? string>> settings = null)
47:     {
48:         builder.Add(new DbConfigurationProvider(setup?? settings));
49:         return builder;
50:     }
51: }