efcore icon indicating copy to clipboard operation
efcore copied to clipboard

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.

Open dewebeloper opened this issue 10 years ago • 17 comments

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.

dewebeloper avatar Jun 09 '16 08:06 dewebeloper

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

natemcmaster avatar Jun 09 '16 16:06 natemcmaster

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

This is constructor.

dewebeloper avatar Jun 09 '16 17:06 dewebeloper

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.

natemcmaster avatar Jun 09 '16 17:06 natemcmaster

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 avatar Jun 09 '16 18:06 dewebeloper

@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?

natemcmaster avatar Jun 09 '16 19:06 natemcmaster

Will try to prepare for tomorrow.

dewebeloper avatar Jun 09 '16 20:06 dewebeloper

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

dewebeloper avatar Jun 13 '16 08:06 dewebeloper

@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.

natemcmaster avatar Jun 13 '16 18:06 natemcmaster

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 avatar Mar 23 '17 14:03 ericwj

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

smitpatel avatar Mar 23 '17 15:03 smitpatel

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 avatar Mar 24 '17 05:03 ericwj

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

ajcvickers avatar Mar 24 '17 17:03 ajcvickers

Removing the parameterless constructor works.

ericwj avatar Mar 24 '17 20:03 ericwj

You can use below Code in DbContext Class:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test"); }

Bahmanpourgholami avatar May 01 '21 19:05 Bahmanpourgholami

I was able to setup the ASP.NET Core web application as an Authorization Server and able to make a request with all kind of Client Core websites. The Problem here is that we have websites developed using old legacy webforms using 4.8 framework.

Web Forms application as a client, I would like to make a request to Authorization Server Core app. Here I am running few issues with setting up ApplicationDBContext using Sql Server

public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { }

public static ApplicationDbContext Create()
{
    return new ApplicationDbContext();
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.UseOpenIddict();
}

When I try to check in startup.cs

image I tried another way of setting of sql server,

image

When I try to install reference Microsoft.EntityFrameworkcore.SQLServer , I’m getting the below error message image

vijaybadugu avatar Sep 20 '23 16:09 vijaybadugu

Just chiming in here to add that if your db provider is an InMemory db, you will receive this error when running migrations. You have to have your migration build use whatever your ultimate database. Makes sense when you think about it, but certainly took me too long of reading this and similar threads before figuring it out.

kcanders avatar Mar 13 '24 21:03 kcanders

its no working maybe Identity Core is working

Saidazizvich avatar May 13 '25 19:05 Saidazizvich