CRUD means Create, Read, Update and Delete. In this beginner project, a Razor Pages website stores student records in a SQLite database. You will generate five pages, test each action, and learn which parts of the generated code protect your data.
Before you start: Complete the Razor Pages introduction and the form validation lesson. The EF Core migrations lesson provides more database background.
Razor form → Page handler → EF Core DbContext → SQLite file
1. Create the web project
Install the supported .NET 10 SDK. The project name matters because the sample namespace below uses it.
dotnet new webapp -n StudentDirectory -f net10.0
cd StudentDirectory
2. Create a Student model
Add Models/Student.cs. EF Core uses Id as the key. The attributes set form rules. A nullable integer for Year keeps a missing answer distinct from zero.
using System.ComponentModel.DataAnnotations;
namespace StudentDirectory.Models;
public class Student
{
public int Id { get; set; }
[Required]
[StringLength(60, MinimumLength = 2)]
public string Name { get; set; } = string.Empty;
[Required]
[EmailAddress]
public string Email { get; set; } = string.Empty;
[Required]
[Range(1, 5)]
public int? Year { get; set; }
}
3. Add EF Core and the page generator
Install SQLite, EF Core design tools and the Razor Pages scaffolder. Microsoft's generator also needs the SQL Server provider while generating pages, although this app runs with SQLite. Keep major versions aligned with .NET 10.
dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 10.0.*
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 10.0.*
dotnet add package Microsoft.EntityFrameworkCore.Design --version 10.0.*
dotnet add package Microsoft.EntityFrameworkCore.Tools --version 10.0.*
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 10.0.*
dotnet tool install --global dotnet-aspnet-codegenerator --version 10.0.*
dotnet tool install --global dotnet-ef --version 10.0.*If either tool is already installed, use dotnet tool update --global with its tool name and --version 10.0.*.
4. Generate the student pages
Run the generator from the project folder. It creates a database context and five pages under Pages/Students: Index, Details, Create, Edit and Delete. The command below follows Microsoft's macOS/Linux form. On Windows, use Pages\Students as the output folder.
dotnet aspnet-codegenerator razorpage -m Student -dc StudentDirectory.Data.SchoolContext -udl -outDir Pages/Students --referenceScriptLibraries -dbProvider sqliteBuild the project and retry if the generator cannot find the model. Confirm that Program.cs registers SchoolContext with UseSqlite. Check the connection string in appsettings.json:
"ConnectionStrings": {
"SchoolContext": "Data Source=School.db"
}
5. Create the database
A migration records schema changes. Applying it creates or updates the SQLite file.
dotnet ef migrations add InitialStudents
dotnet ef database update
dotnet runOpen /Students on the local address printed by dotnet run. Keep generated migration files in source control.
6. Test all four actions
- Create: add Asha Patil with a valid email and year 2. Submit a blank name and check the error.
- Read: return to the list and open Details. Check that the values match.
- Update: edit Asha's year to 3, save, and revisit Details.
- Delete: open Delete, check the name on the confirmation page, then confirm. The student should disappear.
Safety check: Generated pages are a starting point. In Create and Edit handlers, accept only fields visitors may change. Do not bind administrator-only fields from a form. Check ModelState.IsValid before saving. Keep antiforgery protection on POST forms. Add authentication and authorization before allowing real visitors to edit or delete records.
Practice task
Add a Course property with a maximum of 40 characters. Create another migration, update the database, and add the field to Create, Edit, Details and Index. Test a value that is too long. Which parts changed: the model, the database, or both?
Next: Add search and sorting to the student list, then learn how EF Core tracks changes. For more detail, read Microsoft's Razor Pages with EF Core introduction and CRUD walkthrough.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.