Several extensions for IConfiguration that help retrieve connection strings, primarily designed for SQL Server (MSSQL) as I'm using SqlConnectionStringBuilder under the hood.
You can have config like this:
{
"SqlConnectionString": {
"Server": "localhost,1433",
"Database": "TestDb"
}
}Or like this:
{
"SqlConnectionString": "localhost,1433;Database=TestDb"
}And you can get it using config.GetSqlConnectionString().
To specify your own section use key parameter in any method:
{
"SqlServer": {
"Server": "localhost,1433",
"Database": "TestDb"
}
}Get it by config.GetSqlConnectionString("SqlServer").
There is also method that has predefined defaults for development environments. Your empty config:
{
}Using this config.GetDevelopmentSqlConnectionString() empty config will be equivalent to:
{
"SqlConnectionString": {
"Server": "localhost,1433",
"User Id": "SA",
"Password": "1tsJusT@S@mpleP@ssword!",
"MultipleActiveResultSets": true,
"TrustServerCertificate": true
}
}Of course you can override any of this values by providing them in config.
My extensions built on top of SqlConnectionStringBuilder and instead of raw connection string you can get builder. Instead of GetSqlConnectionString use GetSqlConnectionStringBuilder and instead of GetDevelopmentSqlConnectionString use GetDevelopmentSqlConnectionStringBuilder.