From Pluralsight/ASP.NET 6 Core Fundamentals
overview
ASP.NET Core can scaffold. This creates:
- a
DbContextclass—the main class that coordinates EF functionality for a given data model. - Razor pages that handle CRUD operations for an entity.
how to scaffold from visual studio
- Create Pages/subfolder
- In Solution Explorer > right-click Pages/subfolder > Add > New Scaffolded Item a. Installed > Common > Razor Pages > Razor Pages using Entity Framework (CRUD) > ADD
- In Add Razor Pages using Entity Framework (CRUD) dialog > a. Model class drop-down > select Type b. Data context class row > + i. Change data context name to desired name (should end in “Context”) > Add > Add
how to scaffold from command line
- Install required packages:
dotnet add package Microsoft.EntityFrameworkCore.SQLite dotnet add package Microsoft.EntityFrameworkCore.SqlServer # scaffolding tool requires SQL Server even if app does not dotnet add package Microsoft.EntityFrameworkCore.Design dotnet add package Microsoft.EntityFrameworkCore.Tools dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design dotnet add package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore - Create Pages/subfolder
- Install aspnet-codegenerator scaffolding tool:
dotnet tool install --global dotnet-aspnet-codegenerator - Run scaffolding:
dotnet aspnet-codegenerator razorpage -m Student -dc ContosoUniversity.Data.SchoolContext -udl -outDir Pages\Students --referenceScriptLibraries -sqlite
what scaffolding creates
The above scaffolding operation produce the following:
- Under
Pages/subfolder:
Create.cshtmlandCreate.cshtml.cs
Delete.cshtmlandDelete.cshtml.cs
Details.cshtmlandDetails.cshtml.cs
Edit.cshtmlandEdit.cshtml.cs
Index.cshtmlandIndex.cshtml.cs Data/SomeContext.cs- Adds the context to dependency injection in
Program.cs - Adds a database connection string to
appsettings.json