.NET Core

.NET Core Scaffolding

Similar to Ruby on Rails, .NET has a really cool command line interface (CLI) utility for easily creating boilerplate code such as MVC controllers and web pages (Razor) given an entity class.  This can save you numerous hours of repetitive copy and paste during a large project.

What You’ll Need

  • .NET Core SDK
  • Text Editor (Visual Studio Code recommended)

Create New Project for Playing

*Code may be found in GitHub here .

We’ll be using a simple Razor Pages web app with a single entity to manage a catalog of our favorite beers, persisted to a SQLite database.  The terminal command below will create a new webapp in the directory named BeerApp.

dotnet new webapp --output BeerApp
cd BeerApp
mkdir Models
mkdir Data

Install Scaffolding CLI Tool

Install the command line interface (CLI) tool by opening a command line terminal and using the following commands.

dotnet tool install -g dotnet-aspnet-codegenerator
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Design

dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.SQLite
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

You may have noticed the SqlServer package here.  Also probably just like you, I’m not sure why this is required for scaffolding, but dotnet will give you the error below if you try to scaffold without this package installed.

To scaffold controllers and views using models please install Entity Framework core packages and try again: Microsoft.EntityFrameworkCore.Design, Microsoft.EntityFrameworkCore.SqlServer

Create Entity Class and DB Context

Create a file named Beer.cs inside of the Models directory with the following code.  This is used to map our code to the database table used to store our beer data.

namespace BeerApp.Models
{
    public class Beer{
        public int Id { get; set; }
        public string Name { get; set; }
        public int Rating { get; set; }
    }
}

Create a file named BeerContext.cs inside of the Data directory with the following code.  This is used to create a database context for running CRUD (Create, Read, Update, Delete) operations on the Beer entity.

using Microsoft.EntityFrameworkCore;
namespace BeerApp.Data
{
    public class BeerContext : DbContext
    {
        public DbSet<BeerApp.Models.Beer> Beers { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //Don't hardcode like this in a real app:
            optionsBuilder.UseSqlite("Data Source=beers.db");
        }
    }
}

Add DBContext Service to Startup.cs

Edit the existing ConfigureServices method in the file named Startup.cs by adding the BeerContext service, as shown below.

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    //Add this line:
    services.AddDbContext<BeerContext>();
}

Scaffold Razor Pages

dotnet aspnet-codegenerator razorpage --model Beer --dataContext BeerContext --useDefaultLayout -outDir Pages

If you now go into your Pages directory, you will notice many new files.  Specifically, the scaffolding command created pages for all CRUD operations - Create, Read (Details), Update (Edit), and Delete.

01/04/2020  11:09 PM    <DIR>          .
01/04/2020  11:09 PM    <DIR>          ..
01/04/2020  11:09 PM             1,218 Create.cshtml
01/04/2020  11:09 PM             1,180 Create.cshtml.cs
01/04/2020  11:09 PM               996 Delete.cshtml
01/04/2020  11:09 PM             1,434 Delete.cshtml.cs
01/04/2020  11:09 PM               859 Details.cshtml
01/04/2020  11:09 PM               962 Details.cshtml.cs
01/04/2020  11:09 PM             1,269 Edit.cshtml
01/04/2020  11:09 PM             2,003 Edit.cshtml.cs
01/04/2020  10:32 PM               882 Error.cshtml
01/04/2020  10:32 PM               839 Error.cshtml.cs
01/04/2020  11:09 PM             1,152 Index.cshtml
01/04/2020  11:09 PM               695 Index.cshtml.cs
01/04/2020  10:32 PM               172 Privacy.cshtml
01/04/2020  10:32 PM               532 Privacy.cshtml.cs
01/04/2020  10:32 PM    <DIR>          Shared
01/04/2020  10:32 PM                96 _ViewImports.cshtml
01/04/2020  10:32 PM                35 _ViewStart.cshtml

Migrate Database and Run App

dotnet ef migrations add InitialCreate
dotnet ef database update
dotnet run

Go to http://localhost:5000 and you should see the view screen for the Beer list (your browser may complain about the self signed TLS cert, so you may have to override the request) .  You can create new beers from here.  Once a beer is created, you can click on the record and edit or delete.