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示例:
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
浙公网安备 33010602011771号