DefaultConnectionStringProvider Class
Enables you to resolve connection strings by their names at runtime in .NET Core applications.
Namespace: DevExpress.DataAccess
Assembly: DevExpress.Data.v18.2.dll
Declaration
Remarks
In .NET Core applications, the default connection string provider implementation searches the appsettings.json file in the current directory and reads connection strings from the file’s ConnectionStrings section.
In real-life .NET Core applications, you can store connection strings in different sources, such as appsettings.Development.json, connectionStrings.json, in-memory collection, etc.
You can do the following to make an application read connection strings from a set of different configuration sources:
Create a custom configuration and load connection strings from all the required sources. For instance, create the ConfigurationBuilder class instance and use its extension methods (AddJson, AddInMemoryCollection, etc.) See Configuration in ASP.NET Core for more information.
using System.Collections.Generic; using Microsoft.Extensions.Configuration; public IDictionary<string, string> GetGlobalConnectionStrings() { var connectionStrings = new Dictionary<string, string> { [$"ConnectionStrings:VehiclesInMemory"] = "XpoProvider=SQLite;Data Source=Data/vehicles.db", [$"ConnectionStrings:CarsInMemory"] = "XpoProvider=SQLite;Data Source=Data/cars.db;" }; return new ConfigurationBuilder() .SetBasePath(hostingEnvironment.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true) .AddInMemoryCollection(connectionStrings) .AddEnvironmentVariables() .Build() .GetSection("ConnectionStrings") .AsEnumerable(true) .ToDictionary(x => x.Key, x => x.Value); }
Call the static DefaultConnectionStringProvider.AssignConnectionStrings method at application startup after the UseDevExpressControls method call to register connection strings globally.
using DevExpress.DataAccess; public class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // ... app.UseDevExpressControls(); DefaultConnectionStringProvider.AssignConnectionStrings(GetGlobalConnectionStrings()); // ... } }
Tip
Online Example: Report Designer - How to read connection strings from different configuration sources in an ASP.NET Core application