-2

InvalidOperationException: Unable to resolve service for type 'Context.dbcontext' while attempting to activate 'Api.Controllers.ReportsController'.

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • It looks like you are injecting a dbcontext in your controller, but the compiler can't find it. Usually you need to register your services in Startup.cs (or these days it is all in Program.cs if you don't have a Startup class). It's hard to be any more specific without any details. – topsail Aug 21 '22 at 13:26

1 Answers1

0

In .net6:

Add this line to Program.cs:

builder.Services.AddDbContext<dbcontext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("connectionstring")));

In .net5 or others:

Add this line to Startup.cs:

services.AddDbContext<dbcontext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("connectionstring")));

For more details,you can refer to this document.

Chen
  • 4,499
  • 1
  • 2
  • 9