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.
- We had initially just hardcoded the connection string in the DbContext OnConfiguring method, and then the Migrations command worked well.
- 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.
- 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 likepublic 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. - 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'." - 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 simplyBuild 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:
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:
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.
dotnet ef migrations add MigrationName -p YourDataProject -s YourApiProject
.