Skip to main content

DefaultConnectionStringProvider.AssignConnectionStrings(Func<IDictionary<String, String>>) Method

Register connection strings that the specified function returns globally.

Namespace: DevExpress.DataAccess

Assembly: DevExpress.DataAccess.v24.1.dll

NuGet Packages: DevExpress.DataAccess, DevExpress.Win.PivotGrid, DevExpress.Win.TreeMap

Declaration

public static void AssignConnectionStrings(
    Func<IDictionary<string, string>> connectionStringProvider
)

Parameters

Name Type Description
connectionStringProvider Func<IDictionary<String, String>>

A function that returns connection strings as key-value pairs.

Remarks

In ASP.NET Core applications, the default connection string provider implementation uses the IConfiguration service to read connection strings. Applications created based on ASP.NET Core templates call the CreateDefaultBuilder method in the Program.cs file to register this service. If the IConfiguration service is not registered, the default provider searches the appsettings.json file in the current directory and reads connection strings from the file’s ConnectionStrings section.

You can override the default connection string provider to get connection strings from a set of different configuration sources, such as appsettings.Development.json, connectionStrings.json, in-memory collection, etc.

  1. 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);
    }
    
  2. Call the static 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());
          // ...
      }
    
    }
    
See Also