50

Dynamic Connection String In .NET Core

 5 years ago
source link: https://www.tuicool.com/articles/hit/ZnyMfyU
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

To read connection string/keys from appsettings.json file, follow the below steps.

Put your connection string in appsettings.json file.

In ASP.NET Core, configuration API provides a way of configuring an app based on a list of name-value pairs that can be read at runtime from multiple sources. Please note that class libraries don’t have an appsettings.json by default. The solution is simple — access appsettings.json key-value pairs in your project through Dependency Injection principle in ASP.NET Core.

ZVfeamE.png!web

Write the below code to Startup.cs file.

public IConfigurationRoot Configuration {  
    get;  
    set;  
}  
public static string ConnectionString {  
    get;  
    private set;  
}  
public Startup(IHostingEnvironment env) {  
    Configuration = new ConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile("appSettings.json").Build();  
}

IConfiguration has two specializations,

  • IConfigurationRoot is used for root node. It can trigger a reload.
  • IConfigurationSection Represents a section of configuration values. The GetSection and GetChildrenmethods return an IConfigurationSection.
  • Use IConfigurationRoot when reloading configuration or for access to each provider.

In Configure method of Startup.cs class, put the below line.

ConnectionString = Configuration["ConnectionStrings:DefaultConnection"];

Sensitive configuration settings like connection strings should only be stored outside the version control repository (for example- in UserSecrets or Environment Variables) but hopefully you get the idea.

In Context class, put this method.

Here, simply call our Startup class property using Startup(Class).Property, which you set in startup.cs class.

public static string GetConnectionString()  
{  
   return Startup.ConnectionString;  
}

And that's all. Now, you can use a dynamic connection string in your project.

At the end, your startup.cs file looks like this.

  • .SetBasePath() It sets the FileProvider for file-based providers to a PhysicalFileProvider with the base path.
  • .AddJsonFile() We need to include the  Microsoft.Extensions.Configuration.Json  NuGet package if you want to call the.AddJsonFile() method.
  • ConfigurationBuilder Class Used to build key/value based configuration settings for use in an application.

uqErMjN.png!web

And your context file looks like this,

And using GetConnectionString() method we are going to call Startup class method, namely ConnectionString, which returns our connection string.

UseSqlServer is an extension method in the namespace Microsoft.Data.Entity, so you need to import that into your code like this:

  1. using Microsoft.EntityFrameworkCore;   

rqANneQ.png!web

Output 

zIVNFvn.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK