Record one point per day: Netcore Get profile Appsettings.json

Source: Internet
Author: User

Use Netcore to do projects with EF ORM There are many configuration connection strings, read and use methods on the Web

Because many friends use other ORM such as Sqlsugar,nh,dapper, when reading the connection string, often save the information to a configuration file, such as Appsetting.json,

There are a lot of online reading Appsetting.json are injected, in the ORM read configuration, are in a class library inside, so the injection of the way sometimes not suitable for "personal understanding"

Because of the above scenario, the following method reads the configuration can be used anywhere

Like Netcore's friends, welcome Dabigatran qq:86594082

The source of the subsequent replenishment

1. In the project, create a new Netcore class library, creating the class Configservices

namespacecore.extensions{/// <summary>    ///reading configuration Files/// </summary>     Public classconfigservices { Public StaticIConfiguration Configuration {Get;Set; } Staticconfigservices () {//Reloadonchange = True when Appsettings.json is modified reloadConfiguration =NewConfigurationbuilder (). ADD (NewJsonconfigurationsource {Path ="Appsettings.json", Reloadonchange =true })            .        Build (); }    }}

2. Add custom configuration to Appsettings.json under Web project

{  "DBConnection": {    "mysqlconnectionstring":"server=localhost;database=fyt_ims;uid=root;pwd=123456;charset= ' UTF8 '; Sslmode=none"  },    "Logging": {    "Includescopes":false,    "LogLevel": {      "Default":"Warning"    }  }}

3. Create a new class to read the configuration

namespace fyterp.core.model.configmodel{///<summary>//    Database connection string//    </summary>    public class DBConnection {//<summary>//        MySQL database connection string/        ///</summary> Public String Mysqlconnectionstring {get; set;}        <summary>///        SQL Server database connection string///        </summary> public        string sqlserverconnectionstring {get; set;}}}    

4. Write Test read configuration

namespacefyterp.web.pages{ Public classIndexmodel:pagemodel { PublicDbConnection dbsetting {Get;Private Set; }  Public voidOnget () {//get the DbConnection node in the configuration filedbsetting = ConfigServices.Configuration.GetSection ("DbConnection"). Get<dbconnection>(); }    }}

5. Final read results

Record one point per day: Netcore Get profile Appsettings.json

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.