2

The problem:

The dotnet ef Migrations add MyNewMigration command fails with:

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.

There are many SO posts regarding this issue, and I have read most of them. The one that seems to have the exact same problem is here: EF Core unable to run commands. Error: No database provider has been configured for this DbContext

However, the issue was never resolved. Here are some bullets that sums the investigation up and further down, details about the steps.

  1. We had initially just hardcoded the connection string in the DbContext OnConfiguring method, and then the Migrations command worked well.
  2. We then proceeded to use a static method, that read the appsettings.json file. This worked when running the app, but it did not work when running the Migrations add command, because the connectionString we fetched from the static class, always returned null.
  3. We then moved to use dependency injection, like everyone everywhere suggests doing. in the Startup.cs, we use something like services.AddDbContext<MyDbContext> and in the MyDbContext we have a constructor like public MyDbContext(DbContextOptions<MyDbContext> options) : base(options). The constructor is called, the connectionstring is there, app can run, but Migrations Add fails with same error message as above.
  4. I then tested removing the default empty constructor, just keeping the MyDbContext(DbContextOptions<MyDbContext> options) constructor. The error message from the command was then "Unable to create an object of type 'MyDbContext'."
  5. Since I run the commands from my Data project (where entities, dbcontext etc exists), I tried adding a startup path to the command, like dotnet ef Migrations add MyMigrationStuff --startup-project C:\Git\MyProject\MyProject.Api, still without default/empty constructor. This time, the error message was simply Build failed. I then reinstated the empty construtor, ran same command again: then I get the same error as above.

Startup.cs

   public void ConfigureServices(IServiceCollection services)
    {
        // ... other stuff

        string cString = configuration["ConnectionStrings:MyDb"]; // cString is correct and valid!
        services.AddDbContext<MyDbContext>(options => options.UseMySql(cString, mySqlOptions => mySqlOptions
                .ServerVersion(new ServerVersion(new Version(5, 0, 17), ServerType.MySql))));
        // services.AddDbContext<MyDbContext>(); // without DI as in case 1 and 2 above
    }

cString looks correct when running app: enter image description here

MyDbContext:

public class MyDbContext : DbContext
{
    // DbSet etc...

    public MyDbContext() : base()
    {
    }

    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {
        // If I break here, the options has two Extensions, looks ok, see below.
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
    }
 }

This is the options in the MyDbConstructor: enter image description here

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "ConnectionStrings": {
    "MyDb": "Server=localhost;Port=3308;Database=mydb;User=root;Password=root;"
  }
}

I am running ASP.NET Core 3.1.101, EF Core 3.1.1, visual studio 2019.

2
  • The default empty constructor should not be required. Since you have separate projects, try running the dotnet ef command at the solution root like dotnet ef migrations add MigrationName -p YourDataProject -s YourApiProject.
    – juunas
    Feb 26, 2020 at 9:44
  • Thanks for tip, but that didnt help. Same error - no database provider has... yada. I also tried the --context and --startup-projet flags, then it doesnt find either of them.
    – Ted
    Feb 26, 2020 at 12:33

1 Answer 1

1

you need to install MySQL provider using this cmd:

dotnet add package MySql.Data.EntityFrameworkCore

then add this code to your DBContext class constructor:

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
  options.UseMySQL(Configuration.GetConnectionString("MyDb"));
}

finally, add this line into your startup.cs class instead of what you did put:

services.AddDbContext<MyDbContext>();
1
  • I forgot to mention, that here I assume that you are using MySQL DB, if no, check EF Core docs
    – alim91
    Feb 26, 2020 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.