Learn ASP. NET Core Razor Programming Series 11--Update the new field to the database

Source: Internet
Author: User
Tags management studio sql server management sql server management studio



Learn the ASP. NET Core Razor Programming series Catalog



Learn ASP. NET Core Razor programming series One



Learn ASP. NET Core Razor Programming series two--Add an entity



Learn ASP. NET Core Razor Programming series three--Create a data table and create a project basic page



Learn ASP. Razor Programming Series four--asp.net core Razor list template page



Learn ASP. Razor Programming series five--asp.net core Razor new Template page



Learn ASP. NET Core Razor Programming series six--database initialization



Learn ASP. NET Core Razor Programming series seven--Modify List page



Learn ASP. NET Core Razor Programming series eight--concurrent processing



Learn ASP. NET Core Razor programming series nine--Add query function



Learn ASP. NET Core Razor Programming series 10--Add new field









After studying this article, we'll learn how to use the Entity Framework Code first migration feature to add new fields to the model and add new fields to the database at the same time.



When you use EF code first to automatically create a database, Code first adds a table to the database to help track whether the schema of the database is synchronized with the entity class that generated it. If they are not synchronized, EF throws an exception. This makes it easier to find consistent database tables or code problems.



We then use the Code First Migration feature to update the database.



1. Now let's modify the Seeddata class, add a publishing field to the class, and provide the data. Examples of modifications you can refer to each book's information, as shown below.


context.Book.AddRange (
                     new Book
                     {
                         Name = "Python programming from entry to practice",
                         ReleaseDate = DateTime.Parse ("2018-1-12"),
                         Author = "Eric Mathers",
                         Price = 75.99M,
                         Publishing = "Mechanical Publishing"
                     }, 


2. In the visual Studio 2017 menu, choose Build-Build Solution



3. In the visual Studio 2017 menu, choose the Tools menu--NuGet Package Manager > Package Manager Console. Such as.





4. in the PMC, enter the following command,



Add-migration Publishing





The Add-migration directive informs the framework to do the following:



1) Compare the book entity class to the Book table in the database.



2) Create the code to update the table books table structure in the database. Such as.



3) Visual Studio 2017 is named for the migration file with the new field "publishing" as the current date + field name. Such naming helps to manage it. Such as.





5. After executing the instructions above , in the PMC, enter the following command,



Update-database





7. After executing the above instructions, the publishing field is added to the database. Such as.





8. Although we have added a field to the database, there is no data for this field. Such as.







How do you give the field the same value?


    1. If you are developing, and have modified the initialization class Seeddata class. Then you can refer to ( Learning ASP. NET Core Razor programming series six-database initialization ) to initialize data to the database.
    2. If you delete all the records from the database. You can open the book list page in the browser and then use the delete link to delete the record, or you can use SQL Server Object Explorer (SQL Server Management Studio) to delete the data from the database.




Now that we are running the application in Visual Studio 2017 by pressing F5, the application does not report the same error as in the previous article. After the application is running, open the Create/Edit/list page in the browser to verify that the publishing field's book information is already in the page. Such as.



List page:





To create a page:





Edit page:













Learn ASP. NET Core Razor Programming Series 11--Update the new field to the database


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.