Thursday, June 22, 2017

ASP.NET Core EF Core error: No database provider has been configured for this DbContext

by Steve Endow

Here is the full text of the error:

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<TContext> object in its constructor and passes it to the base constructor for DbContext.


There are many potential causes of this error, depending on how your ASP.NET code is structured, but here is one possible cause that required a fair amount of searching for me to resolve.

I load my SQL connection settings from my appsettings.json, so in my ConfigureServices method, I have this code:

services.Configure<APIConfig>(options => Configuration.GetSection("APIConfig").Bind(options));
                    
//Retrieve SQL connection info from config in appsettings
var config = Configuration.GetSection("APIConfig").Get<APIConfig>();
string gpServer = config.GPServer;
string gpCompanyDB = config.GPCompanyDB;
string gpSystemDB = config.GPSystemDB;

var connectionStringGPCompany = $@"Server={gpServer};Database={gpCompanyDB};Trusted_Connection=True;";
var connectionStringGPSystem = $@"Server={gpServer};Database={gpSystemDB};Trusted_Connection=True;";

services.AddDbContext<GPContext>(o => o.UseSqlServer(connectionStringGPCompany));

services.AddDbContext<GPSysContext>(o => o.UseSqlServer(connectionStringGPSystem));


This should setup my DB contexts properly, so I couldn't figure out why I was still getting the "no database provider has been configured" error.

Well, notice that the the AddDbContext is using options.UseSQLServer() to pull in the connection string.  That's all good.

But if you look at my DbContext class, you might notice something is missing:


I certainly didn't notice it, because I wouldn't have known what to look for.

The thing that is missing is a constructor that will allow dependency injection.


The constructor allows the DbContextOptions object to be passed in via DI.  In hindsight, this makes sense and I understand it, but being new to ASP.NET Core and EF Core, I would have never figured this out.

Fortunately I found this StackOverflow post where some genius spotted the issue:

https://stackoverflow.com/questions/38338475/no-database-provider-has-been-configured-for-this-dbcontext-on-signinmanager-p


Looking at the error message again, we see this text:

"also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext"


Sure enough, the error message did recommend the solution, but like I said, I'm new to this stuff, so I could have read that message 50 times and it wouldn't have meant anything to me.

But now I know...until the next time I forget to add the constructor to my DbContext class...


You can also find him on Twitter, YouTube, and Google+




No comments:

Post a Comment