user3497702
user3497702

Reputation: 841

There was an error running the selected code generator:

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)

Upvotes: 11

Views: 24181

Answers (14)

Ivan Rusev
Ivan Rusev

Reputation: 1

update in 2024: decided to download all of the possible Microsoft.VisualStudio.Web.CodeGeneration packages and somehow it worked. I also cleaned the solution and built it beforehand.

Upvotes: 0

bub2k44
bub2k44

Reputation: 1

Check and see if you have any warnings about nullable types. If you do, you can update your "ProjectName".csproj and remove the line: enable Build the project, and retry.

enter image description here

Upvotes: 0

Saintilien Wilson
Saintilien Wilson

Reputation: 51

I Just update al NuGet Packages installed in my project and it run perfectly.

Upvotes: 0

Jabrel
Jabrel

Reputation: 23

In my case, I only update the dependencies for the same version and It Works :)

Upvotes: 0

Ezekiel IIX
Ezekiel IIX

Reputation: 1

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.

Upvotes: 0

Omar X
Omar X

Reputation: 41

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.

Upvotes: 1

bartek1724
bartek1724

Reputation: 3

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

Upvotes: 0

Tanvir Moghal
Tanvir Moghal

Reputation: 1

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)

Upvotes: 0

chethandb
chethandb

Reputation: 1151

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!!

Upvotes: 1

Chaitanya Krishna
Chaitanya Krishna

Reputation: 21

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.

Upvotes: 2

jeanie77
jeanie77

Reputation: 544

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.

Upvotes: 0

Daisy
Daisy

Reputation: 328

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.

Upvotes: 1

Akın Kaplan
Akın Kaplan

Reputation: 119

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" />

Upvotes: 11

user3497702
user3497702

Reputation: 841

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.

Upvotes: 4

Related Questions