ASP.NET Core: Razor Pages

Quick Start Guide

Start a New Project

Start a new Web Application Project (Either ASP.NET Core 2 or 3). Name the project RazorFilms. This will form part of the namespace that will need to be referenced in the class files of the application.

The application will make use of a Database and the management of the database will be done via Entity Framework. Therefore we need to ensure additional packages are installed.

Package Requirements

  1. Microsoft.EntityFrameworkCore
  2. Microsoft.EntityFrameworkCore.SqlServer
  3. Microsoft.EntityFrameworkCore.tools

Managed through:

Note: Tools needed for migration.

Create a Model

Create a Model by adding a model folder and then a class file ie Film.cs

Model/Film.cs

Tip: The [Key] attribute tells the Model the Id is a primary key. The [Required] attribute tells the Model that the FilmTitle is a required field. Validation on this can then be done server and/or client side.

Storing the Connection String in appsettings.json

Set up Connection String in appsettings.json

appsettings.json

Creating a Context

Setting Up a Context, we'll save it as ApplicationDbContext.cs

ApplicationDbContext.cs

Configuring the Startup.cs

Add Context to the Startup.cs

Dependency injection, via ConfigureServices method:

Startup.cs

Migrate and Build

Use the console to run the migration command and then update the database

Add-Migration Initial
Update-Database

It is important to run both console commands. The Add-Migration command can be given a name by the developer. It this example, it was sensible to call it Initial.