DotNet Core使用脚手架(Scaffold)添加Model(数据模型)
the scaffolding tool produces pages for Create, Read, Update, and Delete (CRUD) operations for the xxxx model.
-
Open a command window in the project directory (The directory that contains the Program.cs, Startup.cs, and .csprojfiles).
-
Install the scaffolding tool:
dotnet tool install --global dotnet-aspnet-codegenerator
-
Run the following command:
dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
The following table details the ASP.NET Core code generator parameters:
Parameter | Description |
---|---|
-m | The name of the model. |
-dc | The data context. |
-udl | Use the default layout. |
--relativeFolderPath | The relative output folder path to create the views. |
--useDefaultLayout | The default layout should be used for the views. |
--referenceScriptLibraries | Adds _ValidationScriptsPartial to Edit and Create pages |
Use the h
switch to get help on the aspnet-codegenerator controller
command:
dotnet aspnet-codegenerator controller -h
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | admin:Sample02 James$ dotnet aspnet-codegenerator controller -name ContactsController -m Contact - dc ContactsContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries Building project ... Finding the generator 'controller' ... Running the generator 'controller' ... Generating a new DbContext class 'ContactsContext' Attempting to compile the application in memory with the added DbContext. Attempting to figure out the EntityFramework metadata for the model and DbContext: 'Contact' info: Microsoft.EntityFrameworkCore.Infrastructure[10403] Entity Framework Core 2.2.3-servicing-35854 initialized 'ContactsContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None Added DbContext : '/Data/ContactsContext.cs' Added Controller : '/Controllers/ContactsController.cs' . Added View : /Views/Contacts/Create .cshtml Added View : /Views/Contacts/Edit .cshtml Added View : /Views/Contacts/Details .cshtml Added View : /Views/Contacts/Delete .cshtml Added View : /Views/Contacts/Index .cshtml Added additional file :Views /Shared/_ValidationScriptsPartial .cshtml |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下