Implementing the. NET Core configuration Provider EF

Source: Internet
Author: User

Learn more about the usage of. NET core configuration in 10 minutes. In addition, I have also open source custom configuration Provideref configuration provider and YAML configuration provider. This article first to talk about the implementation of the EF configuration provider which will involve entityframework core knowledge unfamiliar also does not matter and listen to me slowly.

Configure the execution process

The configuration is used first new ConfigurationBuilder() , and the last Call Build() method is assigned to the Configuration property. Well, let's start with this build method.

What does the build method do? It iterates through all of the configuration sources, and so on. Where does the configuration source come from? AddJsonFile AddCommandLine These extension methods do what they do is add the configuration source to the Configurationbuild. Each configuration source has a build method that returns a provider. The build method that invokes the configuration source when traversing all configuration sources obtains the provider of all configuration sources and is finally passed through the constructor ConfigurationRoot .

Every provider has a Load method Configurationroot's constructor iterates through all the provider calls to its Load method. What needs to be done in the Load method is to convert the configuration in the configuration source to IDictionary<string,string> .

Understanding the process of configuration execution, you can implement your own provider.

EF Storage

The configuration of the JSON configuration provider is stored in the JSON file based on EF's provider configuration is stored in the database because EF is used we don't need to care about what database is being used.

The storage configuration in the database does not support nesting and arrays, only simple key-value pairs in the form of two columns in the corresponding database table. Using EF requires defining an entity storage configuration that contains two columns from a database table with two properties.

Internal class configuration{public string Key {get; set;} public string Value {get; set;}}

You then need to define a ConfigurationDbContext configuration for storage and access.

Internal class configurationdbcontext:dbcontext{Private Efconfigurationoptionsbuilder Builder {get;} Public Configurationdbcontext (Efconfigurationoptionsbuilder options): Base (options.    dbcontextoptions.options) {Builder = Options; Public dbset<configuration> configurations {get; set;}}

EFConfigurationOptionsBuilderis a custom class that contains 2 properties one for specifying the name of the storage configuration table another is used to configure database connections and other configurations.

Efconfigurationprovider

Custom provider can inherit the ConfigurationProvider implementation. In Configurationprovider load is a virtual method custom provider need to implement the Load method.

internal class efconfigurationprovider : configurationprovider{     action<efconfigurationoptionsbuilder> optionsaction { get; }     Public efconfigurationprovider (action<efconfigurationoptionsbuilder> optionsaction)      {        OptionsAction = optionsAction;     }    public override void load ()      {        var builder = new  Efconfigurationoptionsbuilder ();         optionsaction (builder);         using  (Var ctx = new configurationdbcontext (builder))         {             ctx. Database.ensureCreated ();             data = ctx. Configurations.todictionary (T => t.key, t => t.value);         }    }}
Efconfigurationsource

Efconfigurationsource inheritance IConfigurationSource implements the build method to return Efconfigurationprovider in build.

Internal class efconfigurationsource:iconfigurationsource{Private ReadOnly Action<efconfigurationoptionsbuilder    > _optionsaction; Public Efconfigurationsource (action<efconfigurationoptionsbuilder> optionsaction) {_optionsAction = Optio    Nsaction; Public Iconfigurationprovider Build (Iconfigurationbuilder builder) {return new Efconfigurationprovider (_op    Tionsaction); }}
Addentityframework extension methods

Add an extension method for adding the EF configuration source.

public static class efconfigurationextensions{public static Iconfigurationbuilder addentityframework (This Iconfigurat Ionbuilder Builder, action<efconfigurationoptionsbuilder> Setup) {return builder.    ADD (new Efconfigurationsource (Setup)); }}
Configuring provider with EF
var builder = new Configurationbuilder (). Addentityframework (options = options.        TableName = "Configs"; Here you use SQLite as the presentation options.    Dbcontextoptions.usesqlite ("filename=config.db"); }); Configuration = Builder. Build ();

Above I use SQLite demo can also use SQL Server, MYSQL, PostgreSQL and so on. The name of the default configuration table is config.

At last

This project has an open source address on GitHub HT T P S://G i t h u B. Co m/c h e n g Xu L v t u/c x l t.extensions.configuration

Use in a project can execute the following command

Install-package Cxlt.Extensions.Configuration.EF

Or

dotnet Add Package Cxlt.Extensions.Configuration.EF



Implementing the. NET Core configuration Provider EF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.