Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have ASP.NET Core MVC project with .NET 7.
I want put my SQL Server connection string in appsetting file. Then use that in my entire project and the layers.
But I get 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<TContext> object in its constructor and passes it to the base constructor for DbContext.'

How can fix this?

What I have tried:

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

This is the program file setting for dbcontext:
C#
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<DB>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("Con1")));


And this is part of my dbcontext in data access layer:

C#
  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. but in this form, I should repeat my connection string in the db file too.
So when I comment the "protected override void OnConfiguring" I get the provider. so how can fix this and only read the connection string from the appsetting file and why does this problem occur?
Posted
Updated 24-Oct-23 1:31am
v2

1 solution

String keys are "case sensitive". Change this:
C#
builder.Services.AddDbContext<DB>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("Con1")));

.. to this:
C#
builder.Services.AddDbContext<DB>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("CON1")));
 
Share this answer
 
Comments
Member 11400059 22-Oct-23 8:35am    
thanks I do this. But still I get the provider error after remove the OnConfiguring method on dbcontext.
Graeme_Grant 22-Oct-23 8:39am    
I fixed your appsettings issue where your connection string was not read. Your connection string not working with your DB is something different. Time for some debugging. The answer is in the error details. Ask a new question and include all of the error details, including stack traces, etc...
Member 11400059 22-Oct-23 11:52am    
thanks for your help. just before I write this answer, I find one solution. but I do not now if this is correct or not. correctly I remove the parameter less constructor. also remove the override method too. now all of my method that connect to database need DbContextOptions as parameter. So i inject this too all of my controllers, components, layers and all over my project that show error. the problem fix now and connection string only read from appsetting. if this solution correct or we can use the better way too?
also as you said the sting is case sensitive. but now when I change my connection string name in program file to coN1 (the name in appsetting is CON1). the program still work and not case sensitive anymore. –
Richard Deeming 23-Oct-23 7:51am    
Why are you injecting the DbContextOptions<TContext> instance? Just inject the DB instance instead, and let the DI container take care of creating the instance for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900