C
C#2mo ago
morry329#

✅ System.InvalidOperationException: No database provide has been configured for this DbContext

It is all about this newbie 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.
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.
I have no clue what triggered all this. Could kindly point me in the right direction? https://pastebin.com/RbkExv6j This is full code and full exception message for your reference.
Pastebin
/*Model */using System.ComponentModel.DataAnnotations;namespace CRU...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
2 Replies
becquerel
becquerel2mo ago
it's been a while since i messed around with ef core, but two things stand out to me 1. you use both .AddDbContext in your program.cs and then also override OnConfiguring in your JsonContext 2. your JsonContext defines a parameterless constructor what happens if you leave the .AddDbContext call, but remove the OnConfiguring override and parameterless constructor? that is, only leaving this ctor: public JsonContext(DbContextOptions<JsonContext> options) : base(options) { }
Pobiega
Pobiega2mo ago
you are also using two different providers?
builder.Services.AddDbContext<JsonContext>(options =>
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))
builder.Services.AddDbContext<JsonContext>(options =>
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))
and
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

private string conn =
"Server=servername;Database=dbName;Trusted_Connection = True; MultipleActiveResultSets = true";

if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(conn);
}
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

private string conn =
"Server=servername;Database=dbName;Trusted_Connection = True; MultipleActiveResultSets = true";

if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(conn);
}
}
MySql and SqlServer are not the same

Did you find this page helpful?