0

I try to use DbContext from another class but I got exception down below.

"System.InvalidOperationException: '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 added ref project in my api app already.

Program.cs

using DAL.ModelContext;
using Microsoft.EntityFrameworkCore;

builder.Services.AddDbContext<MyDbContext>(options => 
    options.UseSqlServer(builder.Configuration.GetConnectionString("MyDbServer")));

MyDbContext.cs --> I deleted the code.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

}

Thx for your reply.

Benji99
  • 11
  • 1
  • 1
    There are about 5,000 hits just on Stack Overflow for this problem - one of the most common when you start development in Entity Framework. Please take a look and describe what you tried before posting a question – Felix Jul 06 '22 at 17:03
  • Does this answer your question? ['No database provider has been configured for this DbContext' on SignInManager.PasswordSignInAsync](https://stackoverflow.com/questions/38338475/no-database-provider-has-been-configured-for-this-dbcontext-on-signinmanager-p) – Felix Jul 06 '22 at 17:04
  • The question not me. – Benji99 Jul 06 '22 at 17:14

2 Answers2

3

There are two ways to configure the database Provider.

One way is by adding the 'AddDbContext' in the application service (i.e) your program.cs file which you have done.

builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("MyDbServer")));

Here check whether you configured the connection string in the appsettings.json.

"ConnectionStrings": { "MyDbServer": "Server=.;Database=<DbName>;Trusted_Connection=True;MultipleActiveResultSets=true" }

Then Add the below constructor in your Context Class.

public class MyDbContext : DbContext {
   public MyDbContext(DbContextOptions options) : base(options)
       {
       }
}

Other Method is by overriding the 'DbContext.OnConfiguring' method in your Context Class.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("Server=.\\;Database=<DbName>;Trusted_Connection=True;");
        }
    }

Check this out and let me know.

Edel Rex
  • 189
  • 5
0

I've been with this problem with a Test Project. I solved when I explicit declared in command line the LibraryProject and WebApi project, like that:

dotnet ef migrations add InitDatabase --project YourDataAccessLibraryName -s YourWebProjectName -c YourDbContextClassName --verbose.

And then, after, explicit as well with same "scaffold" to update database:

dotnet ef database update --project YourDataAccessLibraryName -s YourWebProjectName -c YourDbContextClassName --verbose.

I hope it could help someone.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77