Entity Framework on Azure Functions with Dependency Injection

Kunal Borkar
Globant
Published in
6 min readJun 9, 2021

Introduction

The purpose of this article is to showcase the use of entity framework on Azure Functions with dependency injection to store data in a SQL database as a backing store. With the release of dependency injection support for Azure functions, working with Entity Framework in Azure Functions feels easier. In software engineering, dependency injection is a technique in which an object receives other objects that it depends on. These other objects are called dependencies. In the typical “using” relationship the receiving object is called a client and the passed (that is, “injected”) object is called a service.

Background

Extract, Transform, and Load (ETL) is a common business function that is often necessary for integration between different systems. Use case of ETL tools is when companies transfer data from legacy systems to an updated system so that it can be compatible with the new infrastructure. Azure Functions are small pieces of code that focus on a specific problem and are ideal for tackling tasks like ETL. This article demonstrates how to create a sample Function App to read data from a CSV file and then store the data to a database using Entity framework on Azure Functions leveraging dependency injection. The Dependency Injection feature of Azure Function makes it very simple to work with Entity Frameworks database contexts within an Azure Functions App.

Below points are covered in this article:

  • Prerequisites
  • Demonstration
  • Use-cases
  • Conclusion
  • References

Prerequisites

Below are the prerequisites for this article:

Azure is a cloud platform designed to simplify the process of building modern applications. Whether you choose to host your applications entirely in Azure or extend your on-premises applications with Azure services, Azure helps you create applications that are scalable, reliable and maintainable.

Azure Functions is a serverless solution that allows you to write less code, maintain less infrastructure and save on costs. Instead of worrying about deploying and maintaining servers, the cloud infrastructure provides all the up-to-date resources needed to keep your applications running.

Entity Framework is an open-source ORM framework for .NET applications supported by Microsoft. Entity Framework enables developers to work with data using objects of domain specific classes without focusing on the underlying database tables and columns where this data is stored.

Demonstration

Below are the high level steps:

  1. Create Azure Function Project
  2. Reference required Nuget packages
  3. Setup the Dependency Injection
  4. Test the Azure Function locally
  5. Publish the Function App

1. Create Azure Function Project

The first step is to create a new project in Visual Studio and choose the Azure Functions template. This will provide you with the skeleton structure for creating your Azure function.

On the next window you can select the type of trigger you want the function to be, for my demo purpose I’ve selected a Timer trigger which will run at a specified time scheduled using ncron.

2. Reference required Nuget packages

Now, in order to use dependency injection you need to verify two things:

  1. The Microsoft.NET.Sdk.Functions is at least 1.0.28
  2. Install the Microsoft.Azure.Functions.Extensions package to provide the API to use dependency injection.

It is important to select the correct version number that’s compatible with the version of .NET you are running on. I am running .NET core 3.1 so that is what I have used.

I’ve also referenced the Microsoft.Azure.Functions.Extensions NuGet package, which gives us access to the dependency injection feature.

My database schema is very simple. Here’s the SQL to create the table I used for testing.

I have also updated my local.settings.json to include a connection string I could use for local testing purposes:

I have created an Entity Framework model for my Data items entity and a custom DbContext

3. Setup the Dependency Injection

To set up dependency injection for our function app we use the FunctionsStartup attribute on the assembly to indicate a startup class that will run when the function app starts. In that class, which inherits from FunctionsStartup we override the Configure method. This allows us to retrieve the SQL connection string from configuration, and register a DbContext in the services, which will allow us to inject the MyDbContext into our function.

Dependency injection now offers us the ability to define our functions in classes which have their dependencies injected into their constructor. So here I have defined a MyTimerFunction class whose constructor takes a MyDbContext that can be used by the functions.

Now we are ready to read from our csv files and insert data into the database. Below we are reading from our csv file and returning data objects which we are then inserting into our table using the previously defined MyDbContext.

4. Test the Azure Function locally

To debug the code locally for a timer trigger we need to add the below method signature.

Sample run output below:

5. Publish the Function App

Once we are sure that the function is working as expected we can go ahead with deploying the Azure function. You will need to modify the connection string depending on what you are using. For the demo purpose I have installed SQL Express on a Redhat VM in Azure and used that as my Database. To deploy the Azure function right click on the Project and select publish.

Then select the target as Azure and click on next. Select the specific target as Azure Function App (Windows) followed by the subscription name and resource group you want to deploy the function app to. You can either use an already existing Function App or create a new one and then click on Finish.

For me as I have used SQL Express on a VM I had to allow traffic on SQL port from all the possible IP addresses that were available to the function app. To get a list of all the IP addresses I have used the below command. You can read about it here.

And that’s it you have successfully made use of Entity Framework on Azure Functions using dependency injection.

You can check out the sample code here.

Use-Cases

There are numerous applications for the above implementation. Traditional approaches often involve setting up dedicated FTP servers then deploying scheduled jobs to parse files and translate them for business use. Serverless architecture makes the job easier because a trigger can fire when the file is uploaded. One more real time application is to make use of APIs to fetch data from third party tools/applications which do not retain data at specified intervals/triggers and store them for future use.

Conclusion

In this article we demonstrated how to inject dependency into Azure Function Apps and import data to storage. The ability to use Dependency Injection in Azure Functions means that solutions can be better written using proven design patterns for better maintainability and testability.

References

--

--