ASP MVC5 Introductory Learning series ⑦

Source: Internet
Author: User

Original: ASP. MVC5 Introductory Learning Series ⑦

Then at the end of the previous article, if the development has just encountered model need to add or reduce the field/attribute, but just you also use the EF code frist through the model to survive the database, then change what to do?

Add a field

We are now going to add a comment field to the movie Entity model class what the hell! We know that the database we live on by default with EF code frist is synchronous so that we can better develop it, and the Entity Framework should throw exceptions if the entity model and database are not synchronized.

Code FRISR set up Model library migrations

In VS we first remove the database, the operation is as follows:

To remove the EF connection, in order for our application to be error-prone, we need to do something like this:

Finish typing in the console form:

After you run the console command, my project structure changes as follows:

One more data Migration Migrations folder, this folder is the code frist migration production, there is a history of the class and a configuration information class. We open the Configuration.cs class with the following appearance:

We re-construct the seed method of the Configuration.cs class, modifying the code as follows:

The next thing is that we need to create a migration dbmigration class, which is responsible for creating a database, and we didn't delete the database created by EF before. Next we enter the following command in the console form, as follows:

The command is to create a new database and initialize it. After the execution of the action you will find that VS will automatically generate a file as follows:

You can see that there are 2 methods in this class, and the code intuitively explains that this class at runtime has one operation that is performed during the demotion process, and the other is the operation during the upgrade process. This class of production contains the schema code to create the database, the next thing! This is how to create the database, and the test data we just added to the database, and then we continue to operate in the console form, with the following operations:

After the command run, we come to luck our application to see the effect, run up our program to see whether the data we just added can be displayed correctly:

How, the effect reached it! About the migration here may have a lot of description is not very clear, so also ask your predecessors friends a lot of advice.

Now get to the point. What if I add a field to the model entity class, for example, I added the following fields to the movie class as follows:

Once you add this attribute, you will immediately think that it is not all the pages to add this thing, and the controller inside the action with Bind[inculd (XXX,XXX,XXX,XX)] These places are going to add this thing, The specific changes are as follows:

Index.cshtml page:

Create.cshtml modified as follows:

Let's run the program right now and see what's going to go wrong:

Come to a big yellow page, why? Because we did not give us the new field in the code to migrate the data, so the program does not know here, so the error, that change Bai, where the error is changed where! We modify the seed method in the Cofiguration class as follows:

Is it going to make a mistake if I finish revising this? The answer is yes, for what? Yes, I'm here to add a new field to the model, but is there a field in the database? Let's take a step-by-step look at it, according to the conventional thinking here is to add a field to the database first, then we add the field with the console, the following:

When we do this, we'll add a field to the model snapshot and when we're done with this, VS will produce a code like this:

Then we need to update the datebase in the console, the following:

After the update, we run our program to see the effect:

Then we modify (oh, modify the page also need to rating this field also add) A rating empty data, look at the effect:

Adding fields in this way requires you to manually add the corresponding code to all pages that use these fields, such as the above on the index/create/edit/delete.cshtml page. About this share of things first introduced here, by modifying the entity model we can see how the entity model and the database synchronization, this article time some tension may be described in some places is not very clear, but also please friends forgive and guidance, where there is not the right place, but also hope that guidance I will be the first time to correct, This is the first thing to come here!

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.