Learning ASP. NET Core Razor programming Series 3-create a data table and a project Basic page, asp. netrazor
1. Create a script tool and perform the initial migration
In this section, you will use the package Management Console (PMC) to update the database:
• Add a Visual Studio Web code generation package. This package is required to run the script engine.
• Execute Add-Migration Initial to generate the code file.
• Execute the Update-Database name to Update the Database.
2. in PMC, enter the following command:
1) Install-Package Microsoft. VisualStudio. Web. CodeGeneration. Design, as shown in.
2) After the preceding command is executed, enter Add-Migration Initial. For example.
3. After the preceding command is executed, the Migrations directory and code used to create the initial database will be added to the project, as shown in. The Code is based on the model specified by DbContext (in Models/BookCntext. cs ). The initial parameters are used for naming migration. You can change it to any name, but by convention you can select the name to describe the migration.
4. After the preceding command is executed, enter Update-Database.Migrations/<Time-stamp> _ Initial. csFile to create a database table. The execution process is as follows.
5. After the execution is completed, the Book table will be created in the database. For example.
2. Create a page for adding, deleting, modifying, and querying the Book Management System
2. Enter the following command in the Command window:
- Dotnet aspnet-codegenerator razorpage-m Book-dc BookContext-udl-outDir Pages \ Books -- referenceScriptLibraries
If an error is returned:
The executable file that matches the command "dotnet aspnet codegenerator" is not found. For example.
3. log out of Visual Studio 2017 and run the command again.
If you receive the following error message:
Update Microsoft. VisualStudio. Web. CodeGeneration. Design to 2.0.0. If the version is later than 2.0, downgrade to 2.0.0.
4. Run the command again. This execution is successful, for example.
5. After running, the added cshtml will be generated in your Page \ Books folder. For example.
III. The following table details the parameters of the ASP. NET core code generator:
Parameters |
Description |
-M |
Object Class Name. |
-Dc |
Data context. |
-Udl |
Use default Layout |
-OutDir |
Relative Output Folder path used to create a view |
-- ReferenceScriptLibraries |
Add_ValidationScriptsPartial |
UsehSwitch acquisitionaspnet-codegenerator razorpageCommand help:
Enter the following command in the Command window:
Dotnet aspnet-codegenerator razorpage-h
4. Test the application
1. Press F5 in Visual Studio 2017 to run the application. For example.
2. Add/Books (http: // localhost: 53416/Books) to the url in the address bar of the browser ). For example.
3. Click the "Create New" link, enter the information in the New interface, and then click the "Create" button.
4. Return to the List page in the browser, as shown in.
5. Select a book from the book list and click the "Edit" link. For example.
6. Modify the information on the editing page and click Save. For example.
7. Select a book from the book list and click the "Details" link. For example.
8. Select a book from the book list and click the "Delete" link. For example.
NOTE: If an SQL exception is received, check whether the database has been migrated and updated.