ASP.NET Core MVC web app: controller wizard issue

Viewed 55

I have the following problem ... Visual Studio 2022 walkthrough code https://github.com/everstudybee/DatabaseTest

The walkthrough includes three projects:

  1. Database - Class Library
  2. TestProject - ASP.NET Core MVC web app
  3. TestProject2 - ASP.NET Core MVC web app

There are database classes for the TestProject project in the Database project. Unfortunately, after selecting the wizard, I get the following error. When I configure the controller and views manually, it all works.

  1. Controller selection
  2. Controller configuration
  3. Error

When I do not separate the databases into a separate project, but everything is in one, the wizard works beautifully and creates controllers and views, as seen in the TestProject2 project.

Overall, creating a database with Add-Migration and Update-Database works fine. There is only a problem with this controller and views wizard. Maybe there is a way to do it?

1 Answers

enter image description here

The issue relates the Microsoft.VisualStudio.Web.CodeGeneration.Design package. I could reproduce the problem using 6.0.9 version, 6.0.8 version and 6.0.0 version. But it works well on asp.net 5 and Microsoft.VisualStudio.Web.CodeGeneration.Design 5.0.2.

This is a known issue: in asp.net 6 application, if we add the class library reference in the MVC application, and in the MVC application using Scaffolded to generate page, it will show this error. You can also feedback this issue to the Scaffolding forum, hope they can fix it in the next version.

As a workaround, you can try to the following methods:

Method 1:

  1. Remove the Database class library reference from the MVC project
  2. According to the models in the Database class library, create models (use the same name) in the MVC projects.
  3. In MVC projects, you can use Scaffold to add the generate pages with the new created models.
  4. Remove the models from the MVC project and re-add the Database class library project reference. [Note] You need to change the model reference in the generated page.

Method 2: You can also create a new project which contains the MVC and Database models(put them in the same project), then generate the page using Scaffolded, after that you can copy the generated page to the original/main project.

Method 3

Try to downgrade the Asp.net 6 application to Asp.net 5(remember to downgrade the relates package, especially the Microsoft.VisualStudio.Web.CodeGeneration.Design package), then use Scaffold to generate the related views, after that migration the application from Asp.net 5 to Asp.net 6.

Refer to Migrate from ASP.NET Core 5.0 to 6.0

Related