From Pluralsight/ASP.NET 6 Core Fundamentals
Overview
ASP.NET Core can scaffold. This creates:
- a
DbContext
class—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.cshtml
andCreate.cshtml.cs
Delete.cshtml
andDelete.cshtml.cs
Details.cshtml
andDetails.cshtml.cs
Edit.cshtml
andEdit.cshtml.cs
Index.cshtml
andIndex.cshtml.cs
Data/SomeContext.cs
- Adds the context to dependency injection in
Program.cs
- Adds a database connection string to
appsettings.json