| title | Scaffold a data model with dotnet scaffold in a Razor Pages project |
|---|---|
| description | Scaffold a data model with dotnet scaffold in a Razor Pages project |
| author | rick-anderson |
| ms.author | riande |
| monikerRange | >= aspnetcore-9.0 |
| ms.date | 3/15/2025 |
| ms.topic | article |
| content_well_notification | AI-contribution |
| ai-usage | ai-assisted |
| uid | data/dotnet-scaffold-rp |
The CLI tool, dotnet scaffold creates data access UI for many .NET project types, such as API, Aspire, Blazor, MVC, and Razor Pages. dotnet scaffold can be run interactively or as a command line tool via passing parameter values.
Install the .NET SDK.
The following command installs the scaffolder globally:
dotnet tool install --global Microsoft.dotnet-scaffold
See How to manage .NET tools for information on .NET tools and how to install them locally.
To launch the interactive tool, run dotnet scaffold. The UI changes as more features are added. Currently, the interactive UI looks similar to the following image:
To navigate the UI, use the:
- Up and down arrow keys to navigate the menu items.
- Enter key to select the highlighted menu item.
- Select and enter Back to return to the previous menu.
If you have any problems with the following steps, see Tutorial: Create a Razor Pages web app with ASP.NET Core and select the Visual Studio Code tab.
- Run the following commands to create a Razor Pages project and navigate to the projects folder:
dotnet new webapp -o MyWebApp cd MyWebApp - Add the
Contactclass to theMyWebAppproject: :::code language="csharp" source="~/data/scaffold_RP/samples/MyWebApp/Contact.cs"::: - Run
dotnet scaffoldin theMyWebAppfolder and select Razor Pages, then enter return. - Navigate to Razor Pages with Entity Framework (CRUD) (dotnet-scaffold-aspnet), then enter return.
- Enter return on the selected MyWebApp (MyWebApp.csproj).
- Enter return on Contact (Contact).
- Enter
ContactDbContext, then enter return. - Navigate to a Database Provider, then enter return.
- Select CRUD, then enter return.
- Select a choice for prerelease packages, then enter return.
The dotnet scaffold tool makes the following changes to the project files:
- A package reference is added for Entity Framework.
Program.csis updated to initialize the database connection.appsettings.jsonis updated with connection information.ContactDbContext.csis created and added to the project root directory.- Razor Pages for CRUD operations are added to the Pages folder.
The content has been generated but the database isn't initialized. Run the following commands to initialize the DB.
dotnet tool uninstall --global dotnet-ef
dotnet tool install --global dotnet-ef
dotnet ef migrations add initialMigration
dotnet ef database update
In The preceding commands:
dotnet tool uninstall --global dotnet-efuninstalls thedotnet-eftool. Uninstalling ensures the latest tool is successfully installed. Ifdotnet-efisn't installed, an error messages A tool with the package Id 'dotnet-ef' could not be found. You can ignore this message.dotnet tool install --global dotnet-efinstalls globally thedotnet-eftool.dotnet ef migrations add initialMigrationadds the initial migration. For more information, see Create the initial database schema using EF's migration featuredotnet ef database updateapplies the migrations to the database.
Run the app:
- Enter
dotnet runon the command line, which launches the app. - Open a browser and navigate the URL specified in the output Now listening on: http://localhost:wxyz, where
wxyzis the port listed. - Append
/ContactPagesto the end of the URL. - Test the app by selecting:
- Create New to create a new app.
- Try the Edit, Details, and Delete links.
