Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net 6 Multiple Connection Strings

How to setup in Net 6 program.cs a multiple connection strings? I want to work with Development, Staging, and Production environments, all of them pointing to different database servers.

NET 6. Program.cs:

builder.Services.AddDbContext<MyContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));

});

Thanks in advance.

like image 413
Will Avatar asked Nov 16 '25 14:11

Will


2 Answers

Here's what you could do.

First, create an appsettings.json like this:

appsettings.json

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://*:5000"
      }
    }
  },
  "WillAppConfig": {
    "ActiveEnvironment": "Development",
    "DevDatabase": "server:123.123.123.123, user: will, pass:1234",
    "STGDatabase": "server:123.123.123.123, user: will, pass:1234",
    "ProdDatabase": "server:123.123.123.123, user: will, pass:1234"
  }
}

Then create a class somewhere in your project, that will serve to map the configuration to an object.

WillAppConfigurationMap.cs

public class WillAppConfigurationMap
{
    public string ActiveEnvironment { get; set; }
    public string DevDatabase { get; set; }
    public string STGDatabase { get; set; }
    public string ProdDatabase { get; set; }

}

Finally in your Program.cs, you could select the connection string to use depending on the value of ActiveEnvironment.

var builder = WebApplication.CreateBuilder(args);
WillAppConfig = builder.Configuration.GetSection("WillAppConfig").Get<WillAppConfigurationMap>();
var connectionString = "";
if (WillAppConfig.ActiveEnvironment == "Development")
{
     connectionString = WillAppConfig.DevDatabase
} 
else if (WillAppConfig.ActiveEnvironment == "Staging") 
{
    connectionString = WillAppConfig.STGDatabase
} 
else if (WillAppConfig.ActiveEnvironment == "Production") 
{
   connectionString = WillAppConfig.ProdDatabase
}

builder.Services.AddDbContext<MyContext>(options =>
{ 
    options.UseSqlServer(connectionString));
});
partial class Program
{
    public static WillAppConfigurationMap WillAppConfig { get; private set;}
}

You can remove the "Kestrel" section from the appsettings.json if you don't use it. You can use this approach to map any appsettings.json structure.

Then, you can access your configuration object from ANYWHERE in your app doing Program.WillAppConfig.

like image 76
Cristopher Rosales Avatar answered Nov 18 '25 05:11

Cristopher Rosales


Create multiple connection strings with different names in appsetting.json:

builder.Services.AddDbContext<MyContext>(options => {   
    options.UseSqlServer(builder.Configuration.GetConnectionString(UseNameofConnectionString));
});

Also you can create extension method which gives you required connectionString when you call it.

like image 45
n___dv Avatar answered Nov 18 '25 05:11

n___dv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!