|
| 1 | +--- |
| 2 | +title: Scaffold a data model with dotnet scaffold in a Razor Pages project |
| 3 | +description: Scaffold a data model with dotnet scaffold in a Razor Pages project |
| 4 | +author: rick-anderson |
| 5 | +ms.author: riande |
| 6 | +monikerRange: '>= aspnetcore-9.0' |
| 7 | +ms.date: 3/15/2025 |
| 8 | +ms.topic: article |
| 9 | +content_well_notification: AI-contribution |
| 10 | +ai-usage: ai-assisted |
| 11 | +uid: data/dotnet-scaffold-rp |
| 12 | +--- |
| 13 | + |
| 14 | +# Scaffold a data model with dotnet scaffold in a Razor Pages project |
| 15 | + |
| 16 | +The CLI tool, [dotnet scaffold](https://www.nuget.org/packages/Microsoft.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. |
| 17 | + |
| 18 | +## Install and update the scaffolding tool |
| 19 | + |
| 20 | +Install the [.NET SDK](https://dotnet.microsoft.com/download). |
| 21 | + |
| 22 | +The following command installs the scaffolder globally: |
| 23 | + |
| 24 | +```dotnetcli |
| 25 | +dotnet tool install --global Microsoft.dotnet-scaffold |
| 26 | +``` |
| 27 | + |
| 28 | +See [How to manage .NET tools](/dotnet/core/tools/global-tools) for information on .NET tools and how to install them locally. |
| 29 | + |
| 30 | +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: |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +To navigate the UI, use the: |
| 35 | + |
| 36 | +- Up and down arrow keys to navigate the menu items. |
| 37 | +- Enter key to select the highlighted menu item. |
| 38 | +- Select and enter **Back** to return to the previous menu. |
| 39 | + |
| 40 | +## Create and scaffold a data model in a Razor Pages project |
| 41 | + |
| 42 | +If you have any problems with the following steps, see [Tutorial: Create a Razor Pages web app with ASP.NET Core](/aspnet/core/tutorials/razor-pages/) and select the **Visual Studio Code** tab. |
| 43 | + |
| 44 | +1. Run the following commands to create a Razor Pages project and navigate to the projects folder: |
| 45 | + ```dotnetcli |
| 46 | + dotnet new webapp -o MyWebApp |
| 47 | + cd MyWebApp |
| 48 | + ``` |
| 49 | +1. Add the `Contact` class to the `MyWebApp` project: |
| 50 | + :::code language="csharp" source="~/data/scaffold_RP/samples/MyWebApp/Contact.cs"::: |
| 51 | +1. Run `dotnet scaffold` in the `MyWebApp` folder and select **Razor Pages**, then enter return. |
| 52 | +1. Navigate to **Razor Pages with Entity Framework (CRUD) (dotnet-scaffold-aspnet)**, then enter return. |
| 53 | +1. Enter return on the selected **MyWebApp (MyWebApp.csproj)**. |
| 54 | +1. Enter return on **Contact (Contact)**. |
| 55 | +1. Enter `ContactDbContext`, then enter return. |
| 56 | +1. Navigate to a Database Provider, then enter return. |
| 57 | +1. Select **CRUD**, then enter return. |
| 58 | +1. Select a choice for prerelease packages, then enter return. |
| 59 | +
|
| 60 | + The `dotnet scaffold` tool makes the following changes to the project files: |
| 61 | +
|
| 62 | +- A package reference is added for Entity Framework. |
| 63 | +- `Program.cs` is updated to initialize the database connection. |
| 64 | +- `appsettings.json` is updated with connection information. |
| 65 | +- `ContactDbContext.cs` is created and added to the project root directory. |
| 66 | +- Razor Pages for CRUD operations are added to the Pages folder. |
| 67 | +
|
| 68 | +The content has been generated but the database isn't initialized. Run the following commands to initialize the DB. |
| 69 | +
|
| 70 | +```dotnetcli |
| 71 | +dotnet tool uninstall --global dotnet-ef |
| 72 | +dotnet tool install --global dotnet-ef |
| 73 | +dotnet ef migrations add initialMigration |
| 74 | +dotnet ef database update |
| 75 | +``` |
| 76 | + |
| 77 | +In The preceding commands: |
| 78 | +- `dotnet tool uninstall --global dotnet-ef` uninstalls the `dotnet-ef` tool. Uninstalling ensures the latest tool is successfully installed. If `dotnet-ef` isn't installed, an error messages **A tool with the package Id 'dotnet-ef' could not be found.** You can ignore this message. |
| 79 | +- `dotnet tool install --global dotnet-ef` installs globally the `dotnet-ef` tool. |
| 80 | +- `dotnet ef migrations add initialMigration` adds the initial migration. For more information, see [Create the initial database schema using EF's migration feature](/aspnet/core/tutorials/razor-pages/model&tabs=visual-studio-code) |
| 81 | +- `dotnet ef database update` applies the migrations to the database. |
| 82 | + |
| 83 | +Run the app: |
| 84 | + |
| 85 | +1. Enter `dotnet run` on the command line, which launches the app. |
| 86 | +1. Open a browser and navigate the URL specified in the output **Now listening on: http://localhost:wxyz**, where `wxyz` is the port listed. |
| 87 | +1. Append `/ContactPages` to the end of the URL. |
| 88 | +1. Test the app by selecting: |
| 89 | + 1. **Create New** to create a new app. |
| 90 | + 1. Try the **Edit**, **Details**, and **Delete** links. |
| 91 | + |
| 92 | +## Additional resources |
| 93 | + |
| 94 | +- [dotnet scaffold repo on GitHub](https://github.com/dotnet/Scaffolding) |
| 95 | +- [How to manage .NET tools](/dotnet/core/tools/global-tools) |
| 96 | +- [Microsoft.dotnet-scaffold](https://www.nuget.org/packages/Microsoft.dotnet-scaffold) NuGet package. |
| 97 | +- [Detailed tutorial on EF scaffolding Razor Pages](/aspnet/core/data/scaffold_rp) |
0 commit comments