user10282694
user10282694

Reputation:

how to override local connection string with azure connection string

I am using appsettings.json in .Net core project for connection string. My connection string is :

  "ConnectionStrings": {
    "OT_DB_Connection": "Data Source=108.***.**.**;Initial Catalog=O*******s;User ID=O*******s;Password=O*********$"
  },

In startup.cs i am accessing connection string with key like this

   options.UseSqlServer(Configuration.GetConnectionString("OT_DB_Connection"));

I deployed this code on azure and i have sql database on azure. After deployment how my website will use the connection string of azure ? How to override the local connection string with azure connection string at run time.

Upvotes: 3

Views: 2256

Answers (1)

Mehdi Ibrahim
Mehdi Ibrahim

Reputation: 2624

You should read the following article:

Multiple Environment Configuration Files in ASP.NET Core

You can have multiple appSettings e.g. 1 for you local environment and 1 for Azure etc. When you publish your app to Azure, you can add an application setting called ASPNETCORE_ENVIRONMENT and add a value that maps to your environment for your app to pick up the correct configuration. If you have an appSettings.Azure.json file you can set ASPNETCORE_ENVIRONMENT to Azure and it will use that configuration file.

If you do not want to take this approach, you can also override the connection string directly in Azure as show in the picture below. This is accessible under your app service -> Application Settings -> Connection Strings. You will want to override OT_DB_Connection.

enter image description here

Upvotes: 3

Related Questions