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.

Solution

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 =>
    {
    options1.UseLazyLoadingProxies(false);
    options1.UseSqlServer(
    config["ConnectionStrings:DefaultConnection"],
    builder =>
    {
    builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
    builder.CommandTimeout(10);
    }
    );
    });


    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))
    {
    _applicationDbContext.Entities.Add(
    new DataLayer.Entities.Entity()
    {
    Name = name,
    Description = "Created in the Azure Function"
    }
    );
    }
    _applicationDbContext.SaveChanges();

And that’s all!

The whole thing looks somewhat like in the screenshot below:

How to inject EF Core DbContext in an Azure Function?

Caveats

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!

https://github.com/dodyg/practical-aspnetcore/tree/3.1-LTS/projects/azure-functions/DbContextExample

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:

mm
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments