One of the exciting developments from Build this year was support for dependency injection in Azure Functions. This means you can register and use your own services as part of functions. While you’ve been able to use Entity Framework Core in the past, the pairing with dependency injection makes it a much more natural fit. Let’s create a simple Azure Function that can interact with stateful data using Entity Framework Core. I’m going to create a very simple API that can get and set blog data in an Azure SQL Database.
Adding entity framework to a function project
First up, I created a brand new v2 (.NET Core 2) Azure Functions project. I selected the HTTP Trigger template to start with. In order to use dependency injection I need to verify two things:
Microsoft.NET.Sdk.Functionsis at least
- Install the
Microsoft.Azure.Functions.Extensionspackage to provide the API to use dependency injection
Update-Package Microsoft.NET.Sdk.Functions Install-Package Microsoft.Azure.Functions.Extensions
While we’re at it, let’s install the Entity Framework libraries. ⚠️ These versions are very particular with the version of .NET you will be running. So choose your version of these libraries thoughtfully. At the time of writing this Azure is running .NET Core 2.2.3 so that’s what I’ll use.
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.3
I also want to take advantage of the design-time migration, so I’ll install the tools needed to drive that.
Install-Package Microsoft.EntityFrameworkCore.Design -Version 2.2.3 Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.2.3
Add the entity models
Next, I’ll create a very simple
DbContext for the data models I want to interact with. Let's just stick with the simple Blog and Posts entities.
Write the function code to inject the entity context
I need to verify that I’m using constructors in my function classes so I can inject in the right dependencies (in this case, the entity framework context). This means getting rid of static and adding a constructor. I’ll also modify the constructor to accept a
BloggingContext created above (which itself will need a
To register services like
BloggingContext, I create a
Startup.cs file in the project and implement the
FunctionsStartup configuration. Notice I use an attribute to signal to the functions host where it can run my configuration.
Enable design-time DbContext creation
As mentioned, I want to use the design-time DbContext creation. Since I’m not using ASP.NET directly here, but implementing the Azure Functions
Configure method, entity framework won't automatically discover the desired
DbContext. So I'll make sure to implement an
IDesignTimeDbContextFactory to drive the tooling.
Getting the project .dll in the right spot
There’s one last thing I need to do. When you build an Azure Functions project,
Microsoft.NET.Sdk.Functions does some organizing of the build artifacts to create a valid function project structure. One of the .dlls it moves to a sub-folder is the project .dll. Unfortunately, for the design-time tools like entity framework migration, it expects the .dll to be at the root of the build target. So to make sure both tools are happy, I'll add a quick post-build event to copy the .dll to the root as well. I added this to my
.csproj file for the project.
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy /Y "$(TargetDir)bin\$(ProjectName).dll" "$(TargetDir)$(ProjectName).dll"" />
Adding an entity framework migration
I went into the Azure Portal and created a simple Azure SQL database. I copied the connection string into the
local.settings.json file with a new value for
SqlConnectionString. You can see in my previous code samples I used that as the environment variable that would have the connection string. This way I don't have to check it into source control 🥽.
To make sure the connection string is also available to the CLI tooling for migrations, I’ll open my Package Manager Console and set the environment variable before adding a migration and updating the database.
$env:SqlConnectionString= "Server=tcp:mySqlServerStuffxxx" Add-Migration InitialCreate Update-Database
When browsing to the SQL database in the Azure Portal, I can see the tables were successfully created!
Writing the function app and publishing
That’s all the heavy lifting. Now I just write a few simple HTTP triggered functions that can return and add data to the
BloggerContext I injected in. When running the app, I can then call the different
PUT methods to validate data is being persisted and returned correctly.
After publishing, I just make sure to set the appropriate application setting for
SqlConnectionString with my production SQL connection string, and we're in business!
You can see the full project code on my GitHub account
Originally published at https://dev.to on May 28, 2019.