Efcore: 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.

Created on 9 Jun 2016  路  13Comments  路  Source: dotnet/efcore

After replacing EF Core rc 1 with EF Core rc 2 final I get follor 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 object in its constructor and passes it to the base constructor for DbContext.

I have UoW pattern and on initialization on constructor I get DbContext object like on image.
rc2dbcontextissue

In Startup.cs I have next code:

services.AddDbContext<RegistratorDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<AppUser, IdentityRole>(config =>
            {
                config.User.RequireUniqueEmail = true;
                config.Password.RequiredLength = 6;
                config.Password.RequireUppercase = true;
                config.Password.RequireNonAlphanumeric = false;
            })
            .AddEntityFrameworkStores<RegistratorDbContext>();

What I did wrong and how to fix this issue? I need to mention that same pattern works in RC1.
Thanks in advance.

closed-could-not-reproduce type-investigation

Most helpful comment

@ericwj Looks like you might be hitting this: #6826. Try removing the parameterless constructor or adding an IDbContextFactory.

All 13 comments

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.

What are your constructors for RegistratorDbContext?

cref https://docs.efproject.net/en/latest/miscellaneous/configuring-dbcontext.html#using-dbcontext-with-dependency-injection

public RegistratorDbContext(DbContextOptions<RegistratorDbContext> options)
            : base(options)
        { }

This is constructor.

That's the right constructor. Are there any other constructors?

The issue could be in how RegistratorDbContext and DbContextOptions are being resolved. How is your DbContext being injected into App?

For more background, .AddDbContext will also add options as a singleton to your service container. To help us investigate this, pulling the type DbContextOptions<RegistratorDbContext> from your service container to make sure it is available. If it's not, your DI container may be using a parameterless constructor instead to resolve your DbContext.

No, that is only one constructor on RegistratorDbContext.
On App I have this for constructor

public App(IRepositoryProvider repositoryProvider, RegistratorDbContext ctx )
        {
            _context = ctx;
            //CreateDbContext();
            repositoryProvider.DbContext = Context;
            RepositoryProvider = repositoryProvider;
        }

That's place where I get issue from picture above.
This is how I inject App in Startup.cs
services.AddScoped<IApp, App>();

@dewebeloper I don't see any issues in what you have shared so far. My guess is something else is causing the issue. Can you share a standalone repro?

Will try to prepare for tomorrow.

Sorry for the little delay, but here is full repo with issue https://github.com/dewebeloper/RC2DbContextIssue

@dewebeloper the project you shared has a version of "Registrator.Data.dll" that doesn't match the source code. It only has the public RegistratorDbContext(IHttpContextAccessor httpContextAccessor) constructor.

Delete all of your bin/ and obj/ directories and rebuild. When I did this, the error "No database provider has been configured" goes away.

I am having almost the exact same issue.

Should I be using version 1.0.0 of Microsoft.EntityFrameworkCore.Tools.DotNet with netcoreapp1.1 on dotnet --version 1.0.1 and Microsoft.* packages at 1.1.1?

I actually use a fairly minimal Startup class just to bootstrap a DbContext in a different assembly. So I run dotnet ef with the --startup-project parameter. It is dotnet ef that is crashing in Microsoft.EntityFrameworkCore.Internal.DatabaseProviderSelector.SelectServices().

I am seeing all the correct Add and Use methods being called, but as soon as I comment out the second UseSqlServer in MyDbContext.OnConfiguring, I hit this error. I want to get rid of it as it isn't using the connection string from the configuration file and I would want that to stay that way.

Edit This also happens running nuget locals all -clear, dotnet restore, dotnet ef migrations list --startup-project <path>.

@ericwj - Does your context have constructor with DbContextOptions as parameter?

Yes yes yes

public MyDbContext : DbContext {
    public MyDbContext() : base() { }
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
    //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
    //  base.OnConfiguring(optionsBuilder);
    //  // HACK: Requires putting Startup in this project
    //  optionsBuilder.UseSqlServer(Startup.Configuration["Data:DefaultConnection"]);
    //}
    (...)
}

@ericwj Looks like you might be hitting this: #6826. Try removing the parameterless constructor or adding an IDbContextFactory.

Removing the parameterless constructor works.

Was this page helpful?
0 / 5 - 0 ratings