ConfigurationLibrary 1.0.2
See the version list below for details.
dotnet add package ConfigurationLibrary --version 1.0.2
NuGet\Install-Package ConfigurationLibrary -Version 1.0.2
<PackageReference Include="ConfigurationLibrary" Version="1.0.2" />
paket add ConfigurationLibrary --version 1.0.2
#r "nuget: ConfigurationLibrary, 1.0.2"
// Install ConfigurationLibrary as a Cake Addin #addin nuget:?package=ConfigurationLibrary&version=1.0.2 // Install ConfigurationLibrary as a Cake Tool #tool nuget:?package=ConfigurationLibrary&version=1.0.2
Connection helpers for data providers
This library provides a method to obtain a connection string to use with a data provider or Entity Framework Core, intended for desktop applications as ASP.NET Core has this already with more options via dependency injection.
In the configuration file, ActiveEnvironment
points to the environment to connect, development, stage (test) and production.
Well someone may say I only have one database server. Then set the environment to any of the environments than if at a later time more servers are available you are ready.
Simple example for obtaining connection strings.
Get current connection by ActiveEnvironment
ConnectionString()
appsettings.json
{
"ConnectionsConfiguration": {
"ActiveEnvironment": "Production",
"Development": "Dev connection string goes here",
"Stage": "Stage connection string goes here",
"Production": "Prod connection string goes here"
}
}
Above, ConnectionString()
will be for Production
Used with a data provider
public class DataProviderOperations
{
public static async Task<bool> CanConnect()
{
await using var cn = new SqlConnection(ConfigurationHelper.ConnectionString());
await using var cmd = new SqlCommand(){Connection = cn};
cmd.CommandText = "SELECT ContactId, FirstName, LastName FROM dbo.Contact1;";
try
{
await cn.OpenAsync();
return true;
}
catch (Exception)
{
return false;
}
}
}
Used with Entity Framework Core
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(ConfigurationHelper.ConnectionString());
}
}
Basic usage
Set connection strings in appsettings.json
and copy appsettings.json
to the application folder followed by using ConfigurationHelper.ConnectionString()
to set a connection string.
Inspired by
An earlier repository I wrote on connection to databases.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net5.0
- Microsoft.Extensions.Configuration (>= 5.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 5.0.0)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 5.0.0)
- Microsoft.Extensions.Configuration.FileExtensions (>= 5.0.0)
- Microsoft.Extensions.Configuration.Json (>= 5.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ConfigurationLibrary:
Package | Downloads |
---|---|
EntityFrameworkCoreHelpers
Provides methods for connecting to SQL-Server databases for EF Core 5.x |
GitHub repositories
This package is not used by any popular GitHub repositories.
Added methods to ASP.NET Core