Note: this article is published on 07/21/2024.
After I wrote several articles on this site, I found out it seemed almost for every article, I needed to set up a sample application associated with an entity framework if accessing the database. And, every time, I needed to rewrite the setup process from scratch in order for a new reader to follow along easily. Even for introducing a very simple concept, such as Caching, I needed to spend 80% of the time setting up the sample app, and only 20% on introducing the Caching concept itself.
Therefore, I think it is better to write a basic model such as entity framework sample for various approaches, and then I can reuse them when needed. I made a list of the series of articles below, I will write them one by one, while the Entity framework overview and concept will be covered in the article (0):
- Entity Framework (0), Overview
- Entity Framework (1), with .Net MVC, Code-First
- Entity Framework (2), with .Net MVC, Database-First
- Entity Framework (3), with .Net MVC, Model-First
- Entity Framework (4), with .Net Core MVC, Code-First
- Entity Framework (5), with .Net Core MVC, Database-First
- Entity Framework (6), with .Net Core MVC, Model-First
- Entity Framework (7), with .Net WPF, Database-First
- Entity Framework (8), with .NET Core Web API, Stored Procedure
- Entity Framework (9), with .NET Core Web API, Stored Procedure Implementation
- Entity Framework (10), with .Net WebForms, Database-First
- Entity Framework (11), with .Net Core Razor Pages Code-First --- this article
- Entity Framework (12), with New .Net Core MVC Code-First
- Entity Framework (13), with .Net Core Code-First Summary
Note
We write the Entity Framework for MVC module, but the pattern is the same or similar when applying to Web Application or Web API.
Introduction
This article is about Entity Framework with .Net Core Razor Pages Code-First approach. I will have a brief introductions on Razor (syntax) and Razor Pages.
- Razor is an ASP.NET programming syntax used to create dynamic web pages with the C# or VB.NET programming languages. Razor was in development in June 2010 and was released for Microsoft Visual Studio 2010 in January 2011. Razor is a simple-syntax view engine and was released as part of MVC 3 and the WebMatrix tool set. --- ASP.NET Razor - Wikipedia
- What Is Razor Pages? Introduced as part of ASP.NET Core, and now included in .NET 5, ASP.NET Razor Pages is a server-side, page-focused framework that enables building dynamic, data-driven web sites with clean separation of concerns. --- An Introduction To ASP.NET Razor Pages | Learn Razor Pages
- Razor Pages is designed to make common patterns used with web browsers easy to implement when building an app. Model binding, Tag Helpers, and HTML helpers work with the properties defined in a Razor Page class. --- Introduction to Razor Pages in ASP.NET Core | Microsoft Learn
This article will have the same structure with, such as
- Entity Framework (1), with .Net MVC, Code-First
- Entity Framework (4), with .Net Core MVC, Code-First
They have a lot of similar features. We try to emphasize the differences. However, in order for reader to have a consistent reading, we will keep the similar ones as is. This article will based on this Microsoft Tutorial: Get started with Razor Pages in ASP.NET Core | Microsoft Learn.
We will make a sample app step by step,
- Step 1 - Create a .Net Core Razor Page app
- Step 2 - Add a Model
- Step 3 - Scaffold the Movie Model (Set up DbContext and Data Connection)
- Step 4 - Migrate and Update database
- Step 5 - Run the App and Test
At the end, we will have an .Net Core Razor Pages that can consume a database directly through entity framework.
Step 1 - Create a .Net Core Razor Page app
We use the current version of Visual Studio 2022 17.10.3 and .NET Core 8.0 to build the app:
- Start Visual Studio and select Create a new project.
- In the Create a new project dialog, select ASP.NET Core Web App (Razor Page) > Next.
- In the Configure your new project dialog, enter RazorPagesMovie for Project name > Next.
- In the Additional Information dialog, select .NET 8.0 (Long Term Support) in the target framework dropdowns > Create

Note
- In the Additional Information Page, do not select Enable container support. if you do, you might get this kind of error,
- Platform not supported exception localdb is not supported on this platforms
- that is probably required to use linux container to held the database.
- Platform not supported exception localdb is not supported on this platforms
- For details, you may reference here.
Build and run the app, you will see the following image showing the app,

Step 2 - Add a Data Model
Add a data model, an entity class, into the app, with name as Movie:
- In Solution Explorer, right click the Project > Add => New Folder, Name the folder as Models
- Right click Models folder, > Add
- In the Add New item dialog, Select Class, Enter the class name Movie > Add.
- Add the code to the
Movie class:using System.ComponentModel.DataAnnotations; namespace RazorPagesMovie.Models; public class Movie { public int Id { get; set; } public string? Title { get; set; } [DataType(DataType.Date)] public DateTime ReleaseDate { get; set; } public string? Genre { get; set; } public decimal Price { get; set; } }
The Movie class contains:
-
The
IDfield is required by the database for the primary key. -
A [DataType] attribute that specifies the type of data in the
ReleaseDateproperty. With this attribute:- The user isn't required to enter time information in the date field.
- Only the date is displayed, not time information.
-
The question mark after
stringindicates that the property is nullable.

















Join the conversation! Your thoughts help the community grow.