DotNet Core使用脚手架(Scaffold)添加Model(数据模型)

 

 the scaffolding tool produces pages for Create, Read, Update, and Delete (CRUD) operations for the xxxx model.

https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-2.2&tabs=visual-studio-code

 

  • Open a command window in the project directory (The directory that contains the Program.csStartup.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:

ParameterDescription
-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

  

 

posted on   youhui  阅读(696)  评论(0编辑  收藏  举报

编辑推荐:
· 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代理 了,记录一下
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示