I am setting up my first .NET Core application. I am going to user Dapper (1.50.0-rc2) for the ORM.
I have added the following to my appsettings.json file
My walkthrough:
Add ConnectionStrings section in appsettings.json:
"ConnectionStrings": {
"cs1": "Server=xxxx;Port=xxxx;Database=xxxx; User Id=xxxx;Password=xxxx;Pooling=false;",
"cs2": "Server=xxxx;Port=xxxx;Database=xxxx; User Id=xxxx;Password=xxxx;Pooling=false;",
"cs3": "Server=xxxx;Port=xxxx;Database=xxxx; User Id=xxxx;Password=xxxx;Pooling=false;"
},
Create class that represents the connection strings section:
public class ConnectionStringList
{
public string cs1 { get; set; }
public string cs2 { get; set; }
public string cs3 { get; set; }
}
Open Startup.cs and add the above configuration class to the services collection in ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddOptions();
services.Configure(Configuration.GetSection("ConnectionStrings"));
}
Inject IOptions
public SampleController(IOptions connectionStrings)
{
string cs1 = connectionStrings.Value.cs1;
...