7
votes

I have installed VS 2019 on my windows 10. Created ASP.net Core Web Project -> Selected API. When I try to generate controller referring the model and created the context class, It is not generating the controller class but it gives me the following error:

Error, there was an error running the selected code generator

'Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.Web.CodeGeneration.Utils, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. File name: 'Microsoft.VisualStudio.Web.CodeGeneration.Utils, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' at Microsoft.VisualStudio.Web.CodeGeneration.Design.Program.Main(String[] args)

10

10 Answers

7
votes

I also had this problem but the follwing answer worked for me. Select "Manage Nuget Packages" and install these packages

<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration" Version="3.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" ExcludeAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Utils" Version="3.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGenerators.Mvc" Version="3.1.2" />
1
votes

To solve the problem, you can right click on your project, then select Manage Nuget Packages.

Next, Search Microsoft.VisualStudio.Web.CodeGeneratio.Utils, then install it.

After installing the above, It is generating without error.

1
votes

First, check your csproj file. There is no package reference for 'Microsoft.VisualStudio.Web.CodeGeneration.Utils, Version=3.1.2.0'. This is why the error message showing "Could not load file or assembly". Easy to solve. https://www.nuget.org/packages/Microsoft.VisualStudio.Web.CodeGeneration.Utils/ Go to Package Manager Console install it.

0
votes

I was having a similar behavior, but with a different version of the package: 'Microsoft.VisualStudio.Web.CodeGeneration.Utils, Version=3.1.4.0'. The package was already installed (both in Manage NUGet Packages and also in .csproj file). I uninstalled it from Manage NUGet Packages and reinstalled. That solved my issue.

0
votes

Please check if you have all the package on compatible version. This could happen when you have packages of different versions, one of which is not supporting the current functionality/project type.

0
votes

For what it's worth, the below solution is what worked for me.

My Setup: First of all my project setup was different. I had a MyProject.Data and MyProject, and I was trying to scaffold "API Controller with actions, using Entity Framework" on to my API folder in MyProject, and I was getting the error in question. I was using .net 3.1.

Solution: I had to downgrade all the below nuget packages installed on MyProject.Data project

Before downgrade:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.2">
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.2">


 After downgrade:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.11">
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.11">

Then tried again to use scaffolding and it just worked!!

After scaffolding MyProject had the below nuget package versions installed:

<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.11"/>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" /> 

The main problem was that no errors were displayed other than the one that shows up on the dialog box or does not even point us to any logs or nor any helpful documentation are available. If anyone finds one please attach it to this answer. It was very frustrating and this almost ate away half-day of my weekend :(

Hope it helps someone. Happy coding!!

0
votes

I have also face the same problem. It's a problem with different versions of the NuGet Packages. just try to use similar versions of packages.

Microsoft.VisualStudio.Web.CodeGeneration.Utils(5.0.2) Microsoft.VisualStudio.Web.CodeGeneration.Design(5.0.2)

0
votes

I had similar error during ovveride Identity login and register layout. I did uninstall all package from Application.Web and install again. My reinstall libraries from NuGet Packages:

Microsoft.AspNetCore.Authentication.Google
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.AspNetCore.Identity.UI
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Microsoft.VisualStudio.Web.CodeGeneration.Design
0
votes

I had a .Data project and another another .API project in my solution. The auto scaffolded action controller was trying to add Microsoft.VisualStudio.Web.CodeGeneration.Design which goes up to v5.0.2. I had to downgrade all my nuget packages down to 5.0.2 as follows:

Data Project

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.2">
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.2">
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />

API project

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.2">
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.2">
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />

The API action controller with EF core dbcontext was then able to successfully add:

<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />

I hope this helps! All the best.

0
votes

My solution had 5 projects (2 dlls, webservice, db and MVC). I removed all but the MVC project and the problem ceased. Added all the projects back in and the problem did not return. Comparing the old solution file to the new one showed a bunch of changes, difficult to say which change was the fix, but the solution file is much cleaner.