There was an error running the selected code generator in .Net Core 5

I’m working on a new .Net Core 5 web app with user authentication where I need to customize some of the Identity account pages (Login, Register, Forgot Password, etc). Out of the box these pages are built into .Net Core so there’s nothing you need to do to use them. However, if you want to customize any of the account pages you’ll need to scaffold the source of those pages into your project.

To scaffold items in Visual Studio 2019 – (Version 16.8.4 as of today) right the project or parent folder then select “Add –> New Scaffolded Item”. This has worked for me in the past but recently in .Net Core 5 Visual Studio throws this error during the scaffolding process:

“There was an error running the selected code generator: ‘Package restored failed. Rolling back package changes for ‘Your App’.”

I found a workaround for this error by using the dotnet CLI outside of Visual Studio to execute the scaffolding tool. The following steps use the aspnet-codegenerator tool to scaffold the full Identity pages area into your .Net Core 5 app.

  1. Close Visual Studio.
  2. Open a command prompt and change directories to the project location.
  3. Make sure the aspnet-codegenerator tool is installed on your machine by executing this command:
    dotnet tool install -g dotnet-aspnet-codegenerator
  4. Add the Microsoft.VisualStudio.Web.CodeGeneration.Design package to the project if it does not already exist in your project:
    Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design
  5. Run the following command where MyApp.Models.ApplicationDbContext is the namespace to your DbContext:
    dotnet aspnet-codegenerator identity -dc MyApp.Models.ApplicationDbContext

If the command completed without errors that should have fixed the “There was an error running the selected code generator” issue and created the necessary Identity Pages under Areas/Identity/Pages.

MVC Scaffold Identity Pages

dotnet aspnet-codegenerator also has the ability to scaffold only specific files versus all the Identity files if you don’t need the full set by passing in the -files parameter followed by the files you want to create. (Thanks to Nick for giving me a heads up in the comments about this parameter).

dotnet aspnet-codegenerator identity -dc MyApp.Models.ApplicationDbContext –files “Account.Register;Account.Login;Account.Logout”


0 thoughts on “There was an error running the selected code generator in .Net Core 5

  • Thank you for this post. I ran the command as suggest above but receive this error message

    Could not execute because the specified command or file was not found.
    Possible reasons for this include:
    * You misspelled a built-in dotnet command.
    * You intended to execute a .NET program, but dotnet-aspnet-codegenerator does not exist.
    * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

    Turned out there is a typo in the command. I did the following and it worked for me.
    1) dotnet tool install –global dotnet-aspnet-codegenerator
    2) dotnet-aspnet-codegenerator identity -dc MyApp.Models.ApplicationDbContext

    • Thanks for catching that, Kharpoh! It looks like I already had the dotnet-aspnet-codegenerator tool installed on my machine but that should not be assumed. I see you can also call the dotnet-aspnet-codegenerator directly.

      I updated my post to reflect the change.

      Thanks again for letting me know.

  • I just spent six hous trying to solve this! I wonder why it was so hard to find this information, I guess not many are using .net 5 and mvc together?

    Anyways, I just wanted to add that if you want to scaffold only certain files (as you mention is possible), you can do that by giving a “–files” parameter:

    > dotnet aspnet-codegenerator identity -dc MyApp.Models.ApplicationDbContext –files “Account.Register;Account.Login;Account.Logout”

    Thank you again and take care!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>