Dependency Injection as a Parameter in Startup Configure Issue 3.0 .Net Core

Pasupathi G 1 Reputation point
2020-08-19T05:15:52.537+00:00

Hi Team,

We have one issue Like Our Entity framework db context issue while adding dependecy injection as parameter type in Startup class Configure Method. before we were using .Net Core 2.2. It was working fine. but when we upgraded 3.0 then its throwing 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<TContext> object in its constructor and passes it to the base constructor for DbContext."

our code should be

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ReportConnectionContext reportConnectionContext, IServiceProvider serviceProvider, IJobServiceWeb jobService)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

        ////UpdateDatabase(app);  

        reportConnectionContext.Database.Migrate();  

}

But in above configure service i am using below code

public void ConfigureServices(IServiceCollection services)
{

        services.AddDbContext<ReportConnectionContext>();  
        services.AddRazorPages();  

}

18608-image.png

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,107 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yi E Wang 646 Reputation points
    2020-08-20T06:25:55.957+00:00

    To solve this issue, please change the statement to register ReportConnectionContext service in ConfigureServices to:

    services.AddDbContext<ReportConnectionContext >(options => options.UseSqlServer(DbCoreConnectionString));

    This need Microsoft.EntityFrameworkCore.SqlServer dll , you can download in NuGet Packages.

    And please make sure your ReportConnectionContext class is constructed as follows:

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

    You can also refer to this discussion for more details: https://github.com/dotnet/efcore/issues/4825

    0 comments No comments