Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implement the EF of the .NET Core configuration Provider

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The use of. NET Core configuration is described in detail in "you can learn .NET Core configuration in 10 minutes." in addition, I have also opened up custom configuration ProviderEF configuration Provider and Yaml configuration Provider. In this article, let's first talk about the implementation of EF configuration Provider, which will involve the knowledge of EntityFramework Core. It doesn't matter if you are not familiar with it. Listen to me slowly.

Configuration execution process

When using the configuration, new ConfigurationBuilder () is used first, and finally the Build () method is called to assign a value to the Configuration property. So let's start with this Build method.

What does the Build method do? it traverses all the configuration sources, etc. Where did the configuration sources come from? remember the AddJsonFile,AddCommandLine extension methods? what they do is add the configuration sources to the ConfigurationBuild. Each configuration source has a Build method that returns a Provider. The Build method of the configuration source is called when traversing all the configuration sources, and the Provider of all the configuration sources is finally passed to the ConfigurationRoot through the constructor.

There is a Load method in every Provider. The constructor of ConfigurationRoot iterates through all the Provider to call its Load method. What needs to be done in the Load method is to convert the configuration in the configuration source to IDictionary.

Once you understand the process of configuration execution, you can start to implement your own Provider.

EF storage

The configuration of Json configuration Provider is stored in the Json file and the configuration of EF-based Provider is stored in the database because we do not need to care about what database is used because we use EF.

The storage configuration in the database does not support nesting and arrays are just simple key-value pairs that correspond to two columns in the database table. To use EF, you need to define an entity storage configuration that contains two attributes corresponding to two columns in the database table.

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

Then you need to define a ConfigurationDbContext to store and access the configuration.

Internal class ConfigurationDbContext: DbContext {private EFConfigurationOptionsBuilder Builder {get;} public ConfigurationDbContext (EFConfigurationOptionsBuilder options): base (options.DbContextOptions.Options) {Builder = options;} public DbSet Configurations {get; set;}}

EFConfigurationOptionsBuilder is a custom class that contains two properties-one to specify the name of the storage configuration table and the other to configure database connections and other configurations.

EFConfigurationProvider

Custom Provider inherits the ConfigurationProvider implementation. Load is a virtual method in ConfigurationProvider. Custom Provider needs to implement the Load method.

Internal class EFConfigurationProvider: ConfigurationProvider {Action OptionsAction {get;} public EFConfigurationProvider (Action 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 inherits IConfigurationSource to implement the Build method to return EFConfigurationProvider in Build.

Internal class EFConfigurationSource: IConfigurationSource {private readonly Action _ optionsAction; public EFConfigurationSource (Action optionsAction) {_ optionsAction = optionsAction;} public IConfigurationProvider Build (IConfigurationBuilder builder) {return new EFConfigurationProvider (_ optionsAction);}} AddEntityFramework extension method

Add an extension method to add EF configuration sources.

Public static class EFConfigurationExtensions {public static IConfigurationBuilder AddEntityFramework (this IConfigurationBuilder builder, Action setup) {return builder.Add (new EFConfigurationSource (setup));}} configure Providervar builder = new ConfigurationBuilder () .AddEntityFramework using EF (options = > {options.TableName = "configs"; / here use SQLite as the demonstration options.DbContextOptions.UseSqlite ("Filename=config.db");}); Configuration = builder.Build ()

Above I use SQLite demonstration can also use SQL Server, MySql, PostgreSQL, and so on. The name of the default configuration table is Configuration.

Last

This project has opened up the address ht t p s: / / github on github. Co m / c h en g xu l v t u / C x l t.Extensions.Configuration

In a project, you can execute the following command

Install-Package Cxlt.Extensions.Configuration.EF

Or

Dotnet add package Cxlt.Extensions.Configuration.EF

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report