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