0

I have declared connection string in appsetting.json file. I would like to change the connection string through web api call.

Is there a way to do it?

I am working with ASP.Net Core.

appsettings.json:

  "Database": {
    "ProviderName": "MySQL",
    "ConnectionString": "server=localhost;database=sampledb;uid=user;pwd=user"
  },

Startup.cs

  var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);  

  //Accessing Database section from appsettings.json
  services.Configure<dbSettings>(StaticSettings.GetSection("Database"));
Dot Net Dev
  • 574
  • 8
  • 20
  • https://stackoverflow.com/questions/41653688/asp-net-core-appsettings-json-update-in-code – Marcus Höglund Jan 31 '18 at 20:01
  • you need a tenant middleware: https://stackoverflow.com/questions/69248827/how-to-load-dynamic-dbcontext-based-on-database-name-obtained-from-another-dbcon/69249006 – mohammadreza Sep 20 '21 at 05:46

1 Answers1

0

Firstly, in development environment the right place to store connection string is in User Secrets. In Visual Studio, Right click on Project > Manage User Secrets, opens secrets.json. User Secrets option works only on your dev. machine.

While if your app is running on Azure, Azure Portal has application settings options to define connection strings for your app.

Coming to your question, you can store multiple connection strings in application settings. For example:

 "ConnectionStrings": {
    "dev_db": "Server=devSQLServerName;Database=dev;User Id=userName;Password=yourPassword",
    "test_db": "Server=testSQLServerName;Database=test;User Id=userName;Password=yourPassword",
    "staging_db": "Server=stagingSQLServerName;Database=staging;User Id=userName;Password=yourPassword",
    "production_db": "Server=prodSQLServerName;Database=staging;User Id=userName;Password=yourPassword"
}

Now, write a method to get the connection string based on your environment.

    public string GetDBConnectionString(string environment)
    {
        string connectionString = string.Empty;

        if (environment == "production")
        {
            connectionString = Configuration["ConnectionStrings:production_db"];
        }
        else if (environment == "development")
        {
            connectionString = Configuration["ConnectionStrings:dev_db"];
        }
        else if (environment == "test")
        {
            connectionString = Configuration["ConnectionStrings:test_db"];
        }
        else if (environment == "stage")
        {
            connectionString = Configuration["ConnectionStrings:staging_db"];
        }

        if (connectionString == null)
        {
            throw new Exception("Could not locate production DB connection string for env: " + environment );
        }
        else
        {
            return connectionString;
        }
    }

Now call this method from your Web API's action method or where ever you want to consume.

Nagesh Andani
  • 432
  • 6
  • 12