Entity Framework Core logo

How to fix “No database provider has been configured for this DbContext” in EF Core?

This post was most recently updated on April 4th, 2024.

I’m using Entity Framework in most of the projects that I’m working on – and for the most part, it’s a pleasure to work with. But as always, there are plenty of chances to run into issues… So obviously, I stumbled into all of the rare stuff that IS broken! This one I hadn’t really seen before: “No database provider has been configured for this DbContext”.

This time I encountered the issue while setting up a new project and getting started working on the model. EF Core simply fell on its back and started screaming like a 2-year-old who didn’t get another juice box that they wanted.

Sometimes it just feels tough to get started, right? Well, let’s see what do we have today and how to resolve it!

AI-powered summary: EF Core Configuration Solutions for Developers

Encountering ‘No database provider has been configured for this DbContext’ in EF Core can be daunting. This article guides through the resolution process, offering practical steps to overcome initial migration hurdles and set up a new project effectively. It emphasizes the importance of proper provider configuration by overriding the DbContext.OnConfiguring method or using AddDbContext. The solutions presented are straightforward, aiming to aid developers in achieving a stable and functional database context.

Problem

So, we have another Entity Framework Core problem on our hands! Namely, while you’re running something like this (probably as your initial migration for a new database context) in your Visual Studio Package Manager Console:

Add-Migration Initial

You get an error like this:

No database provider has been configured for this DbContext.

Or even a more verbose version like this:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. 

If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

Okay – so how do we fix this?

Solution

Let’s jump to it, then!

Time needed: 5 minutes

How to fix “No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider.”?

  1. Make sure you’re running the Add-Migration commandlet against the right project

    Depending on your project configuration, you’ll need to run your Add-Migration command against a project where you instantiate your DBContext, perhaps with the project hosting your DBContext as your startup project and/or working directory.

    You can see the details on how to verify most of the details here:
    https://www.koskila.net/an-assembly-specified-in-the-application-dependencies-manifest-was-not-found-when-running-add-migration-in-entity-framework-core/

  2. Override the OnConfiguring method

    In your DbContext class, add something like this:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
    // You don't actually ever need to call this
    }


    This overridden method call will get called when your context gets instantiated (when running Add-Migration, anyone) – but you’ll then need to next configure the actual provider for this context.

  3. Add a call to your preferred database provider

    This can be mocked – just have something like the below in the OnConfiguring call:

    optionsBuilder.UseSqlServer(@”Server=(localdb)\mssqllocaldb;Database=Contoso-dev”);

    Add-Migration won’t actually use this. And as long as the overridden method call isn’t called by you, this connection string won’t be used then, either. Or you could always add some logic around it to not actually use it.

    You could even do something like the below:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
    if (!optionsBuilder.IsConfigured)
    {
    optionsBuilder.UseNpgsql(
    "Host=TimescaleDB;Port=5432;Database=valutecdb;Username=postgres;Password=pwd");
    }
    }

And there we go!


Did it work for you? Got further questions? Let me know in the comments -section below!

5 7 votes
Article Rating
Subscribe
Notify of
guest
6 Comments
most voted
newest oldest
Inline Feedbacks
View all comments