Search code examples
asp.net-coreidentitydbcontextprovider

Why do I get database provider error in ASP.NET Core 7?


I have an ASP.NET Core 7 MVC project. I want put my SQL Server connection string in the appsettings.json file, and then use that in my entire project and the layers.

But I get the the provider error:

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

How can I fix this?

This is my appsettings.json:

"ConnectionStrings": {
    "CON1": "data source=DESKTOP-JU6B74d\\SQL2019;initial catalog = Laser; integrated security = true;TrustServerCertificate=True;"
},

This is the program file setting for the DbContext:

builder.Services.AddControllersWithViews();

builder.Services.AddDbContext<DB>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("CON1")));

And this is part of my DbContext in the data access layer:

public class DB : IdentityDbContext
{
    public DB() : base() { }

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

    protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder)
    {
        OptionsBuilder.UseSqlServer(@"data source=DESKTOP-JU6B74d\SQL2019;initial catalog = Laser; integrated security = true;TrustServerCertificate=True;");
        base.OnConfiguring(OptionsBuilder);
    }
}

When I use this DbContext, I am not getting the provider error and program worked without problem. but in this form, I should repeat my connection string in the DbContext file, too.

So when I comment out the

protected override void OnConfiguring

method, I get the provider error. How can fix this and only read the connection string from the appsettings.json file - and why this problem even occurring?


Solution

  • You can refer to Examine the generated database context class and registration to create the database context class and registration.

    database context class:

    public class MvcMovieContext : DbContext
        {
            public MvcMovieContext (DbContextOptions<MvcMovieContext> options)
                : base(options)
            {
            }
        }
    

    The constructor uses Dependency Injection to inject the database context (MvcMovieContext) into the controller:

    public class MoviesController : Controller
    {
        private readonly MvcMovieContext _context;
    
        public MoviesController(MvcMovieContext context)
        {
            _context = context;
        }
    }
    

    Besides , Configuration keys are case-insensitive. For example, ConnectionString and connectionstring are treated as equivalent keys. You can have a look at Configuration keys and values to know more.