The following tutorial is based on "Get started with Razor Pages in ASP.NET Core" from docs.microsoft.com.
- Visual Studio 2022 for Mac
- In the Visual Studio for Mac Installer, install the .NET Core target.
- Tutorial 1- Create a Razor Page application
In this section, you'll be adding classes to manage movies in a database.
-
In Solution Pad, right-click the RazorPagesMovie project. Select Add > New Folder. Name the folder
Models
. -
Right click the
Models
folder. Select Add > New Class. -
Select General > Empty Class and name the class
Movie
. -
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; } }
Now, let's add NuGet package for the Sqlite provider for Entity Framework Core, which will enable you to have a database for your web app.
-
In Solution Pad, right-click the RazorPagesMovie project and select Manage NuGet Packages.
-
Search for
Microsoft.EntityFrameworkCore.Sqlite
. -
Check the checkbox for the package and select Add Package.
Repeat these steps to add the following packages:
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.VisualStudio.Web.CodeGeneration.Design
-
Add a folder named Data.
-
Create a new class named
RazorPagesMovieContext.cs
in theData
folder.
The database context, or DbContext
, is a class provided by Entity Framework to facilitate database interactions. Add the following code:
using Microsoft.EntityFrameworkCore;
using RazorPagesMovie.Data;
namespace RazorPagesMovie.Data;
public class RazorPagesMovieContext : DbContext
{
public RazorPagesMovieContext(DbContextOptions<RazorPagesMovieContext> options)
: base(options)
{
}
public DbSet<Movie> Movie => Set<Movie>();
}
The previous code creates a DbSet
property for the entity set. An entity set typically corresponds to a database table, and an entity corresponds to a row in the table.
Open the appsettings.json
file and add the RazorPagesMovieContext
connection string as shown in the following code:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowHosts": "*",
"ConnectionStrings": {
"RazorPagesMovieContext": "Data Source=MvcMovie.db"
}
}
-
Open the
Program.cs
file. -
Add the following using directive at the top of the file.
using RazorPagesMovie.Data;
-
Add the following code under
builder.Services.AddRazorPages();
:builder.Services.AddDbContext<RazorPagesMovieContext>(options => options.UseSqlite(builder.Configuration.GetConnectionString("RazorPagesMovieContext") ?? throw new InvalidOperationException("Connection string 'RazorPagesMovieContext' not found.")));
To run commands to create and manage migrations, you need to install the dotnet ef
tool. Do that with the following command in the terminal (you can open a terminal inside of Visual Studio for Mac by right clicking on the project and selecting Open in Terminal).
dotnet tool install --global dotnet-ef
Tip: If
dotnet-ef
is already installed, you can update it withdotnet tool update --global dotnet-ef
.
For more information, see Entity Framework Core tools reference - .NET Core CLI.
In the terminal, run the following commands in the project directory:
dotnet ef migrations add InitialCreate
dotnet ef database update
Commands Explained
Command | Description |
---|---|
add package |
Installs the tools needed. |
ef migrations add InitialCreate |
Generates code to create the initial database schema based on the model specified in 'RazorPagesMovieContext.cs'. InitialCreate is the name of the migrations. |
ef database update |
Creates the database. |
Install the aspnet-codegenerator
global tool by running the following command:
dotnet tool install --global dotnet-aspnet-codegenerator
Tip: If
dotnet-aspnet-codegenerator
is already installed, you can update it withdotnet tool update --global dotnet-aspnet-codegenerator
.
Note: You need to close and reopen the console window to be able to use this tool.
Run the following command:
dotnet aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages/Movies --referenceScriptLibraries
-
Build the application with Build > Rebuild Solution.
-
Run the application with Debug > Start without Debugging.
-
Append
/movies
to the URL in the browser: https://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, see the Part 3, scaffolded Razor Pages in ASP.NET Core article.
NEXT TUTORIAL: Modifying generated pages