Where To Instantiate Database Connection In Azure Functions

Steps to Instantiate Database Connection In Azure Functions

In this Azure tutorial, we will discuss Where To Instantiate Database Connection In Azure Functions.

Where To Instantiate Database Connection In Azure Functions? Define the Database connection string under the Application Settings in the Azure Portal using the below steps

  • Log in to the Azure Portal
  • Navigate to the Azure Function App
  • Click on the Configuration option from the left navigation on the Azure Function App page
  • Click on the + New Application Settings button or if you are using the Entity Framework, click on the + New Connection String to add the database connection string.

Where To Instantiate Database Connection In Azure Functions

We will discuss an important topic, i.e., Where To Instantiate Database Connection In Azure Functions, while you are working with the Azure Function and interacting with the databases from the Azure Function.

There are two scenarios here: Either you will develop the Azure Function locally with the help of any IDE like Visual Studio, and you are trying to interact with the database. So in that case, you need to define the database connection strings in the local.settings.JSON file, and then you can call that key from your Azure Function code.

One important thing here is it will help you for your development and testing purposes locally. Once you deploy your Azure Function into Azure, you will not get the reference of the local.settings.json file in Azure, so again, you have to manually define the database connection string details under the application settings section in the Azure Portal.

Most importantly, you need to remember one thing: the local.settings.JSON file is not for Azure. It is only for your local development and testing purposes.

So ultimately, when you are working with the Azure function, you will have to interact with the database, Even if you are developing the Azure Function in Visual Studio or any command-line tools or even if you are developing the Azure Function in the Azure Portal, you need to define your database connection strings under the Application Settings in the Azure Portal.

To define the Connection string in the Azure Portal, you need to follow the below steps

Login to the Azure Portal (https://portal.azure.com/)

Now, Once you have successfully logged in to the Azure Portal, navigate to the Azure Function App for which one you are working and for the one you want to set the Database connection string.

On the Azure Function App page, you need to click on the Configuration from the left navigation and then click on the + New Connection String

Note: One important thing to note down here is, If You are working with Entity Framework, then click on the + New connection String like below to add the database connection string, If you are using any other than Entity framework then Use the + New Application Setting button to add the database connection string.

Where To Instantiate Database Connection In Azure Functions

On the Add/Edit connection string window, Provide a name for the connection and the value of the Database connection string. Then, you need to select the Database type and click the OK button.

azure function connect to on-premise database

Now, the connection string for your database will be added like below

Instantiate Database Connection In Azure Functions Azure Portal

So once you define the Database connection string, you need to read that connection string value in your Azure Function Code. You can use the below way to call it

var newconn = System.Configuration.ConfigurationManager
                 .ConnectionStrings["MyDBConnname"].ConnectionString;

This is the way when you are working with the Azure Function using the .Net languages. For other than the .Net language, the syntax will be different.

You might be wondering where you will get the Database connection string value. Follow the below steps to get the Connection string value for the Azure SQL database using the Azure Portal

Login to the Azure Portal (https://portal.azure.com/)

Search for the SQL Database in the Azure Portal and click on the Search result SQL databases

azure function connect to sql database

You will see the list of available SQL databases you have created before. Click on the specific database for which you need to copy the Connection string value.

On the SQL Database page, click on the Connection strings link from the left navigation, and then under the ADO.NET tab, you can see the connection string; next to that, you will find a copy button. Click on the Copy button to copy the connection string as highlighted below.

Manage connections in Azure Functions

Set the connection string in the existing Azure Function project

You might have already deployed your Azure Function Project into Azure. Right-click on the Azure Function solution and click on the Publish option

Click on the Edit Azure App Service settings link as highlighted below.

Set the connection string in the existing Azure Function project

On the below Application Settings window, click on the + Add Setting, Provide the name for the App settings, and then click on the OK button.

Where To Define Database Connection In Azure Functions

Now, Provide the connection string value that we have copied following the above-mentioned steps, and then click on the OK button.

How can I add database connection string to Azure Functions?

Azure Function Connect To On-Premise Database

Using the Hybrid connection option, you can connect to the On-Premise Database from your Azure Function. This is possible with the App Service Plan.

Note: If you want to use the Hybrid connection option, you will get this option if your Azure Function App belongs to the App Service Plan.

To get the Hybrid connection option, You need to follow the below steps

Login to the Azure Portal(https://portal.azure.com/)

Create an Azure Function App using the App Service Plan. To create that, You will have to provide the below options

Azure Function Connect To On Premise Database 1

On the Hosting tab, Choose the plan type as App service plan, provide the other options as highlighted, and then click the Review + Create button.

Connecting SQL DB on premise from Azure using Hybrid Connection Manager

Now, On the next window, click the create button to create the Azure Function App with the App service plan.

Now, Navigate to the Azure Function App you have created just now, click on the Networking option from the left navigation, and then see the Hybrid connections option as highlighted below.

Click on the Configure hybrid connection endpoints

azure function app connect to sql database

Click on the + Add hybrid connection on the below window. On the next window, click on the Create new hybrid connection button.

Azure Functions – Azure Functions Using Hybrid Connections

On the Create new hybrid connection window, Provide a name for the Hybrid connection Name, Specify the Endpoint Host, Provide the Endpoint Port, and then select the Create new option for the service bus namespace. Next, choose the location and name and click on the OK button.

Azure Functions connect to on-premises SQL Server

Now you can see below the Hybrid connection was created successfully

Azure App Service Hybrid Connections

The next step is to Connect manager, which is the main counterpart of the Hybrid connection.

Click the Download connection manager button above the + Add hybrid connection button. It will download the setup file in your local system. You can configure the setup file and choose the Hybrid connection we created above while setting up the connection manager.

You can check out How to configure the Azure Function App settings now.

You may also like following the below Articles

Wrapping Up

Well, in this article, we discussed Where To Instantiate Database Connection In Azure Functions. I hope You have enjoyed this article !!!