I Got an error while creating controller using scaffolding "Unable to resolve service for type, while attempting to activate ‘MyWebApplication.Models.MyDbContext’ "Here is the image of my code and error while creating controller using scaffolding
I want a solution of my problem and I tried everything but unable to resolve. Here is the image of my code and error while creating controller using scaffolding
2
Answers
Please provide more information so that we can help you out. To me, it sounds like you have not registered the DbContext with your Dependency Injection and that’s why it could not be "activated".
Check if your context is registered with the DI first. If that does not make the trick, provide more information.
You can register the context by doing something like:
Based on the error screenshot you provided, I replicated the issue Unable to resolve service for type, while attempting to activate ‘MyWebApplication.Models.MyDbContext’, The issue relates that the DbContext is not registered when using the scaffolding.
My model:
My Dbcontext:
When I didn’t register MyDbcontext in ConfigureServices:
The same problem occurs as depicted in the figure:
When I registered Mydbcontex with ConfigureServices in Program.cs:
The StudentsController be created successfully: