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 and Identity Server 4 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 and Identity Server 4 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.

In the latest version of Visual Studio 2019 – (Version 16.8.4 as of today) you are able to do this by right clicking the project and selected “Add –> New Scaffolded Item” for all .Net Core projects below version 5 without any issues. But if you are trying to scaffold Identity in .Net Core 5 from Visual Studio then you might run into this error:

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

A way around this is error I found was to use the dotnet CLI outside of Visual Studio. Follow these steps to scaffold the full Identity pages into your .Net Core 5 app.

  1. Close Visual Studio
  2. Open a command prompt and change directories to the project where you want to create the scaffolded items.
  3. Run the following command updating MyApp.Models.ApplicationDbContext to your own DbContext: dotnet aspnet-codegenerator identity -dc MyApp.Models.ApplicationDbContext

That should fix the “There was an error running the selected code generator” issue. You can also pass a set of parameters into the dotnet aspnet-codegenerator app if you only want to scaffold a certain number of files versus all Identity files.

Convert A Generic List To CSV File In C#

This is an example of how to convert a generic list to a dynamic downloadable .csv file using C#, the CsvHelper library and the FileStreamResult.

Download the repo and run the full project solution from Github

In this example a request comes into the controller, we create a generic list of type ListItem, created a memory stream and using the CsvWriter library  return a dynamic .csv file to the browser.

Step 1: Create the generic list class
This class will hold our generic list items. This can be whatever data you need.

public class ListItem
    public int Id { get; set; }
    public string Name { get; set; }

Step 2: Create the MVC action to return the CSV file
This action method creates the generic list, writes it to memory with help from the CsvHelper library and returns the FileStreamResult back to the browser.

//Action Method for your controller
public ActionResult ConvertToCSV()
    //Create the test list
    var list = new List<ListItem>()
        new ListItem(){Id = 1, Name = "Jerry"},
        new ListItem(){Id = 2, Name="George"},
        new ListItem(){Id = 3, Name="Kramer"},
        new ListItem(){Id = 4, Name = "Elaine"}

    byte[] result;
    using (var memoryStream = new MemoryStream())
        using (var streamWriter = new StreamWriter(memoryStream))
            using (var csvWriter = new CsvWriter(streamWriter))
                result = memoryStream.ToArray();

     return new FileStreamResult(new MemoryStream(result), "text/csv") { FileDownloadName = "filename.csv" };

h/t to this StackOverflow post

Download the repo and run the full project solution from Github