Kirsten
Kirsten

Reputation: 18198

how do i set the connection string for my web application in azure?

I created a web application using the ASP.Net 5 web application template and published to an Azure web application.

In the Settings section of my web app I was able to create a Data Connection. How do I tell my web application to use this data connection.

[Update] the name of the connection string in my appsettings.json is DefaultConnection

Upvotes: 3

Views: 4959

Answers (3)

user2721607
user2721607

Reputation: 318

Connection strings can now be found in Azure from App Services --> Select the website --> select the webslot if necessary --> Settings Section/Environment Variables

Upvotes: 0

Kirsten
Kirsten

Reputation: 18198

App Services, then select the web app, then Settings, then Application Settings. Scroll down to connection strings. Make sure the connection is named DefaultConnection

Upvotes: 1

Banwari Yadav
Banwari Yadav

Reputation: 506

Please try this...

  1. Go to Azure web app > configure > connection strings.

  2. Add a connection string with the name DefaultConnection.

  3. Use Configuration.Get("Data:DefaultConnection:ConnectionString") to access it.

Example using timesheet_db instead of DefaultConnection This is an example from my own timesheet application. My connection string was named timesheet_db. Just replace all instances of that string with DefaultConnection to adapt the example to your use case.

Azure web app configuration enter image description here

Azure web app service control manager The online service control manager at https://myWebAppName.scm.azurewebsites.net/Env will show the connection strings.

enter image description here

Startup.cs Setup configuration settings in Startup so that the environmental variables overwrite the config.json

public IConfiguration Configuration { get; set; }
public Startup()
{
 Configuration = new Configuration()
    .AddJsonFile("config.json")
    .AddEnvironmentVariables();    <----- will cascade over config.json
}

Configure the database in Startup.

public void ConfigureServices(IServiceCollection services)
 {
  services
    .AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<ProjectContext>(options =>
    {
        var connString =
            Configuration.Get("Data:timesheet_db:ConnectionString");
        options.UseSqlServer(connString);
    });
  }

Of course, the example uses a connection string named timesheet_db. For you, replace all instances of it with your own connection string named DefaultConnection and everything will work.

Upvotes: 3

Related Questions