The tutorial below is based on "Get started with ASP.NET Core Razor Pages in Visual Studio Code" from docs.microsoft.com.
In this section, we are adding classes to manage movies in a database.
- Add a folder named Models
- Add a class to Models folder named Movie.cs
using System;
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; }
}
}
Create a new class named MovieContext.cs
in the Models folder. The database context, or DbContext
, is a class provided by Entity Framework to facilitate database interactions.
using Microsoft.EntityFrameworkCore;
namespace RazorPagesMovie.Models
{
public class MovieContext : DbContext
{
public MovieContext(DbContextOptions<MovieContext> options)
: base(options)
{
}
public DbSet<Movie> Movie { get; set; }
}
}
The code above 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 MovieContext
connection string as shown below.
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"MovieContext": "Data Source=MvcMovie.db"
}
}
Open Startup.cs file and add the code below to the ConfigureServices method.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<MovieContext>(options => options.UseSqlite(Configuration.GetConnectionString("MovieContext")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
Add the following using statements: using RazorPagesMovie.Models
and using Microsoft.EntityFrameworkCore
.
In the command line run the following commands
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet restore
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 'MovieContext.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 --version 2.1.1
Note: You will need to close and reopen the console window to be able to use this tool.
Run the commands below
On Windows
dotnet aspnet-codegenerator razorpage -m Movie -dc MovieContext -udl -outDir Pages\Movies --referenceScriptLibraries
On Mac and Linux
dotnet aspnet-codegenerator razorpage -m Movie -dc MovieContext -udl -outDir Pages/Movies --referenceScriptLibraries
- Run the app
dotnet run
- Launch a browser and go to
http://localhost:5000/movies
- Create a new entry
Extra light read 7 minutes: If you want to read more on pages we just created click here for more information.
NEXT TUTORIAL :Modifying generated pages