Skip to content

Commit 8a6e3e3

Browse files
Merge pull request #34968 from dotnet/main
Merge to Live
2 parents bef0806 + 16ce0de commit 8a6e3e3

File tree

6 files changed

+144
-1
lines changed

6 files changed

+144
-1
lines changed

aspnetcore/data/scaffold_RP.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
![scaffold tool initial](~/data/scaffold_RP/images/scaffold1.png)
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)
100 KB
Loading
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace MyWebApp;
2+
public class Contact
3+
{
4+
public int Id { get; set; }
5+
public string? Firstname { get; set; }
6+
public string? Lastname { get; set; }
7+
public string? Email { get; set; }
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
public class ContactDbContext(DbContextOptions<ContactDbContext> options) : DbContext(options)
4+
{
5+
public DbSet<MyWebApp.Contact> Contact { get; set; } = default!;
6+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.EntityFrameworkCore;
2+
var builder = WebApplication.CreateBuilder(args);
3+
var connectionString = builder.Configuration.GetConnectionString("ContactDbContext") ?? throw new InvalidOperationException("Connection string 'ContactDbContext' not found.");
4+
5+
builder.Services.AddDbContext<ContactDbContext>(options => options.UseSqlite(connectionString));
6+
7+
// Add services to the container.
8+
builder.Services.AddRazorPages();
9+
10+
var app = builder.Build();
11+
12+
// Configure the HTTP request pipeline.
13+
if (!app.Environment.IsDevelopment())
14+
{
15+
app.UseExceptionHandler("/Error");
16+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
17+
app.UseHsts();
18+
}
19+
20+
app.UseHttpsRedirection();
21+
22+
app.UseRouting();
23+
24+
app.UseAuthorization();
25+
26+
app.MapStaticAssets();
27+
app.MapRazorPages()
28+
.WithStaticAssets();
29+
30+
app.Run();

aspnetcore/toc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,9 @@ items:
15051505
- name: Inheritance
15061506
uid: data/ef-mvc/inheritance
15071507
- name: Advanced topics
1508-
uid: data/ef-mvc/advanced
1508+
uid: data/ef-mvc/advanced
1509+
- name: Scaffold a data model with dotnet scaffold in RP
1510+
uid: data/dotnet-scaffold-rp
15091511
- name: EF 6 with ASP.NET Core
15101512
uid: data/entity-framework-6
15111513
- name: Azure SQL Database with Azure App Service

0 commit comments

Comments
 (0)