In my solution, I have my main ASP.NET Core MVC project and two other class library projects where I keep the models and data related stuff. Everything works fine as far as working with the database is concerned, however I cannot create a view from a method:
I have registered the DbContext
service of course in Program.cs
otherwise the whole thing wouldn't work.
What do I need to do for it to add views (it worked when everything was one single project, now it doesn't).
I have registered the DbContext service of course in Program.cs otherwise the whole thing wouldn't work.
What do I need to do for it to add views (it worked when everything was one single project, now it doesn't).
It would be nicer, if you could share more details like program.cs or project structure and how did you configured your DbContext from class library, so that it can be investigate well.
However, based on your error message which indicating the scaffolding process needs to access your DbContext class to generate the view, but it's located in a separate project and not readily available to the project where you're trying to scaffold.
Another important point is that, this might be causing because of dependency resolution issues when trying to scaffold views from methods in your ASP.NET Core MVC project. When you have multiple projects in your solution, it's crucial to ensure that your dependencies are properly configured across projects.
I am not sure, which way you have followed when implementing scaffholding, but when using the dotnet ef command for scaffolding, you can use the --project and --startup-project options to specify the projects involved.
You can do as following:
dotnet ef database add /path/to/your/controller/file --context MyDbContext --project DataLayerProject --startup-project MainProject
Things to keep in mind here is:
Replace /path/to/your/controller/file
with the actual path to your controller file.
Also replace MyDbContext
with the actual name of your DbContext
class.
Replace DataLayerProject
with the name of the project containing your DbContext
class.
Replace MainProject
with the name of your main ASP.NET Core MVC project
Note: You can use of IDesignTimeDbContextFactory
interface. Inside the CreateDbContext method, configure the DbContextOptionsBuilder
and return a new instance of your DbContext. In addition, please refer to this official document.