Say "Dependency Injection" one more time I DARE YOU

How to inject EF Core DbContext in an Azure Function?

This post was most recently updated on November 19th, 2020.

Reading Time: 3 minutes.

Ah, one more on the related topics of different configurations for your Azure Function apps.

In this article, I’ll describe how you can configure the Entity Framework Core database context to be injected into your Azure Functions App. In my experience, this is a very typical and often-needed model to structure your Azure Functions, so while tooling and templates aren’t quite there, it’s worthwhile to document here.


I’ve posted before about how to instantiate a database context using DbContextOptionsBuilder – see a copy-pasteable example right here: How to access EF’s DbContext in an Azure Function

However, if you want to use Dependency Injection (DI) instead, the implementation is just a bit different. Below, I’ll show how!

And that should be it! The example function now looks somewhat like this:

Time needed: 10 minutes.

How to configure injecting a DbContext in an Azure Function?

  1. Add a Startup class (WebJobsStartUp -attribute) to your Azure Function

    The steps to achieve this are described in this earlier blog post, so I’m not repeating them here:

    How to configure Azure Function’s startup?

  2. In the Startup, configure your DbContext like this:

    builder.Services.AddDbContext(options1 =>
    builder =>
    builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);

    The configuration options only shown as an example – not required.

  3. Configure the database context to support being instantiated with options

    Now, you need to add a constructor to your database context class, like below:
    public ApplicationDbContext(DbContextOptions options) : base(options) { }

    Don’t worry about it being empty. EF Core figures out all the details, it just needs a method to work with :)

    If you skip this step, you might run into an error like below:

    Microsoft.EntityFrameworkCore: AddDbContext was called with configuration, but the context type ‘ApplicationDbContext’ only declares a parameterless constructor. This means that the configuration passed to AddDbContext will never be used. If configuration is passed to AddDbContext, then ‘ApplicationDbContext’ should declare a constructor that accepts a DbContextOptions and must pass it to the base constructor for DbContext.
    Value cannot be null. (Parameter ‘provider’)

  4. Configure the DbContext to be injected to your Azure Functions

    This is pretty nifty – since the database context is injected in your app as a service, you can just configure it to be injected at class initialization, directly in the constructor:

    private readonly ApplicationDbContext _applicationDbContext;

    public Function1 (ApplicationDbContext applicationDbContext) {
    _applicationDbContext = applicationDbContext;

    You’ll need using -statements somewhat like these:
    How to inject EF Core DbContext in an Azure Function?
    (DataLayer in the example above is the namespace my database context is in!)

  5. Use the database context in your function!

    Now it’s safe to use your database context in your code. No need to dispose it – it’s tied to your Azure Function App’s lifecycle:

    if (!string.IsNullOrEmpty(name))
    new DataLayer.Entities.Entity()
    Name = name,
    Description = "Created in the Azure Function"

And that’s all!

The whole thing looks somewhat like in the screenshot below:

How to inject EF Core DbContext in an Azure Function?


Note, that there’s a bit of a caveat in here regarding the lifecycle management of your injected objects. Namely, in case you created your function as static, you might need to refactor your code a bit as static functions can’t access instance objects.

How to inject EF Core DbContext in an Azure Function?

Sample project

If you want a full-on working project template, you can grab one from this template directory – I contributed it there for your convenience!

Further reading

I’ve posted an article about a closely related case earlier – although in that case, the Database Context was built using DbContextOptionsBuilder. See that copy-pasteable example right here:

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments