0

Here is the connection string I copied from my Azure portal:

Server=tcp:xxxxxxxxxxx.database.windows.net,9999;Initial Catalog=DbNameDB;Persist Security Info=False;User ID={my_username};Password={my_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30; 


Here is my appsettings.json file:

{ 
    "Logging": { 
        "LogLevel": { 
            "Default": "Warning" 
        } 
    }, 
    "ConnectionStrings": { 
        "DefaultConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=HeroesDB;Integrated Security=True;MultipleActiveResultSets=True" 
    } 
} 


Now that I want my app to work on a production server, how do I set it up?


Should I ...

  1. ...create an appsettings.production.json file in my project (next to my appsettings.json file) and set it's DefaultConnectionString to Azure server's connection string? If I did so, would Azure automatically know which of the two files to use?
  2. ...change my original appsettings.json file's connection string to Azure server connection string?
  3. ...look for a totally different solution? If yes, what would you suggest?


I'm leaning towards solution no 1., but I want to be sure.

1

2 Answers 2

1

In general appsettings.json is the main settings file, i.e. the production file if you want. All settings that are not found in other child files (e.g. appsettings.development.json) are taken from this appsettings.json.

So it should be the reverse: your appsettings.json should contain your production connection string, and appsettings.development.json a connection string to your local development DB.

Now how do you pick the right settings file. The key is ASPNETCORE_ENVIRONMENT setting. If it is not set, than appsettings.json is picked automatically. If it's set, the app looks for appsettings.{environment}.json file.

If your app runs in Azure portal, you set environment in the settings of your deployed web app. Locally (and I assume you use Visual Studio) there is a launchSettings.json file, found under Properties folder of your project in Solution explorer. There you can create a local launch configuration with the right environment, e.g.:

"my-local": {
      "commandName": "Project",
      "launchBrowser": false,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5003/"
    }

This will create a "my-local" start option in the dropdown of the "play" button in VS. If you select "my-local" it will pick this settings, from them the "Development" environment, and from it take appsettings.Development.json and from there your connection string.

I hope I got your question right. To sum up again, locally use an evironment configured for appsettings.development.json and local DB. Deploy to Azure and it will pick up the normal appsettings.json file and run against production DB.

Sign up to request clarification or add additional context in comments.

Comments

0

In your Azure portal, go to your App, and under Settings, find Configuration.

In the configuration, you can add a connection string. This will override the connection string your json file provided it's the same name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.