there was an error running the selected code generator when adding new view

Viewed 3671

I'm working on a asp.net core MVC project. I need to add a view for a method of a controller. To do that, I right click on the body of the considered method and then choose Add View but the system shows me an error:

there was an error running the selected code generator: Package restore failed rolling back package changes MyProject.

I appreciate if anyone tells me how I can fix the issue.

enter image description here

enter image description here

enter image description here

4 Answers

I've been banging my head for a while with this. I resolved it by following these steps.

  1. Make sure all Packages are exactly the same version! Even Patch versions seem to fail. So if your SQLite is 3.1.1 and you SQLServer is 3.1.2 it will fail

  2. Open your csproj file and manually edit the versions of the packages to the exact same version. Currently its 5.0.3 for everything but I had Microsoft.VisualStudio.Web.CodeGeneration.Design at 5.0.2 and it was failing

enter image description here

  1. Optional Step : delete these package from all projects
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.2" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration" Version="5.0.2" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
        
  1. After you manually edit the csproj file, save it/them (make changes in all your csproj files btw)

  2. I also did this step - Nuget Package Manager Settings -> Package Sources Unselect the local folder and only make sure the remote one is selected

Package Sources

  1. Close Visual Studio 2019.

  2. Delete .vs folder in the Solution folder.

  3. Start Visual Studio again, set startup project etc.

  4. Now try creating a scaffolder controller, it will install the packages again and should work!

I had been trying scaffolding identity with latest version of as provided below

 <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.7" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.7" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.7" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.7">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
</ItemGroup>

and I had been getting similar error for latest 2 days, I tried most of the solution but it didn't work out for me.

Later I created a new project and tried scaffolding and it worked, I checked .csproj of new project and configured my old one same as that and it worked for me. Here is the version details if you wish to scaffold using latest versions.

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.7" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.6" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.6" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.6">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
  </ItemGroup> 

may be it can help someone trying identity scaffolding with latest version(5.0.7).

Package restore failed rolling back package changes for 'MyProject'.

It seems trying to install Package(s) that is not compatible with netcore version of your app, which cause the issue.

To troubleshoot the issue, please try to check the detailed information in Output window, like below.

enter image description here

I don't know if this is specific to my case, but I had a problem when it didn't want to generate a api controller. Project ran, No errors, no issues. But it just didn't want to generate.

So what I did was I just rebuilt the solution and then the next time I ran the generator it showed a different error. forgot to write [keyless] on top of my class. I fixed that and then ran the generator again, and it worked like a charm.

Related