The following tutorial is based on "Get started with Razor Pages in ASP.NET Core" from docs.microsoft.com.
-
In the Visual Studio Installer, install the following workload:
- ASP.NET and web development
-
Tutorial 1- Create a Razor Page application
In this section, you're adding classes to manage movies in a database.
-
In Solution Explorer, right-click the RazorPagesMovie project. Select Add > New Folder. Name the folder
Models
. -
Right click the
Models
folder. Select Add > Class. Name the classMovie
. -
Replace the contents of the
Movie.cs
file with the following code:namespace RazorPagesMovie.Models; public class Movie { public int ID { get; set; } public string? Title { get; set; } public DateTime ReleaseDate { get; set; } public string? Genre { get; set; } public decimal Price { get; set; } }
-
In Solution Explorer, right click on the Pages folder. Select Add > New Folder.
-
Name the folder
Movies
. -
In Solution Explorer, right click on the
Pages/Movies
folder. Select Add > New Scaffolded Item. -
In the Add New Scaffolded Item dialog, select Razor Pages on the left pane and select Razor Pages using Entity Framework (CRUD) on the right pane. Then select Add.
-
Complete the Add Razor Pages using Entity Framework (CRUD) dialog:
- In the Model class drop down, select Movie (RazorPagesMovie.Models).
- In the Data context class row, select the + (plus) sign.
- The Add Data Context dialog opens. Leave the default value and select Add. The class name
RazorPagesMovie.Data.RazorPagesMovieContext
is generated.
- The Add Data Context dialog opens. Leave the default value and select Add. The class name
- Select Add.
The generated code from the scaffold process creates the following files:
- Pages/Movies/
- Create
- Delete
- Details
- Edit
- Index
- Data/MovieContext.cs: Class that includes a
DbSet
property for the entity set. An entity set typically corresponds to a database table, and entity corresponds to a row in the table.
The scaffold process also modifies some existing files:
- Startup.cs: Created a DB context and registered it with the dependency injection container.
- appsettings.json: The connection string used to connect to a local database is added.
When we scaffolded the database, SqlServer was selected as the default and is a great option for developer ASP.NET Core applications that need a database. For this application let's switch the database use SQLite, small, fast, self-contained, high-reliability, full-featured, SQL database engine. This is easy to do in just a few step:
-
From the Tools menu, select NuGet Package Manager > Package Manager Console.
-
Run the following command:
Install-Package Microsoft.EntityFrameworkCore.Sqlite
- Update the connection string found in the
appsettings.json
: Change
"ConnectionStrings": {
"RazorPagesMovieContext": "Server=(localdb)\\mssqllocaldb;Database=RazorPagesMovieContext-5fff187f-3f2c-4d38-9b74-1812d4621ed3;Trusted_Connection=True;MultipleActiveResultSets=true"
}
to the following:
"ConnectionStrings": {
"RazorPagesMovieContext": "Data Source=MvcMovie.db"
}
- Update the
AddDbContext
registration in theProgram.cs
from:
builder.Services.AddDbContext<RazorPagesMovieContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("RazorPagesMovieContext") ?? throw new InvalidOperationException("Connection string 'RazorPagesMovieContext' not found.")));
to the following:
builder.Services.AddDbContext<RazorPagesMovieContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("RazorPagesMovieContext") ?? throw new InvalidOperationException("Connection string 'RazorPagesMovieContext' not found.")));
- From the Tools menu, select NuGet Package Manager > Package Manager Console.
- In the Package Manager Console, enter the following commands:
Add-Migration Initial
Update-Database
-
Press F5 to run your app.
-
Append /movies to the URL in the browser: http://localhost:port/movies
-
Create a new entry with the Create link:
It works!
-
Test the Edit, Details and Delete links
If you get a SQL exception, verify you have run migrations and updated the database.
Extra light read 7 minutes: If you want to read more on pages you just created click here for more information.
NEXT TUTORIAL: Modifying generated pages