ASP. net mvc series: adds new attributes to existing models. asp. netmvc

Source: Internet
Author: User

ASP. net mvc series: adds new attributes to existing models. asp. netmvc

Add a new attribute Rating to the model class Movie.

Recompile and regenerate the solution. Press Ctrl + Shift + B to add new data to the corresponding view, as shown in figureIndex. cshtmlMedium

After the editing is complete, the System reports an exception: "System. InvalidOperationException" occurs in EntityFramework. dll, but not in user code.

The cause of this exception is that the Movie class has a more Rating column than the database, so we need to add the Rating field to the database. You only need to open the database file and edit it;

Now we will introduce another method: Use Code First to migrate and update the database.

First, we need to put the database file (Movies. mdf), and then open the "Package Manager Console" (you can find it in "NUGet Package Manager" under the "Tools" menu ), enter Enable-Migrations-ContextTypeName MvcMovie in the console. models. movieDBContext

Run add-migration Initial.

After the solution is successful, you will see an additional Migrations folder and Configuration. cs file in the solution. There is a Seed method in the Configuration class, which will be called after migration to the latest version. So we need to add some initialization values, otherwise the update-database execution will fail, as in

Create an initial object for the database table in Seed

        protected override void Seed(MvcMovie.Models.MovieDBContext context)        {            context.Movies.AddOrUpdate(i => i.Title,              new Movie              {                  Title = "When Harry Met Sally",                  ReleaseDate = DateTime.Parse("1989-1-11"),                  Genre = "Romantic Comedy",                  Price = 7.99M              });        }

Now you can execute update-database. You will see that the console mentions that the Seed method has been run and the database file has been created.

Here, you will wonder why you didn't see the Rating field, because before deleting the database, I commented out the attribute Rating to be added. If you don't comment out the attribute, rating will be automatically added, so we will not add this field, or add another field Note, Screenwriters ...; if you do not comment out Rating during the execution, you can create another attribute to complete the task of adding a new attribute. Here, I still use Rating.

Now we add a new column "add-migration Rating" for the database. After execution, we need to execute update-database.

View the database table structure. We can see the newly added Rating column.

We can add new attributes without deleting the database. You only need to execute the add-migration attribute name and update-database command on the console; now you can run the project directly and it will no longer report errors.

If you encounter errors during the update-database operation, you can delete the Migrations folder and start the operation again.

  http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table

 

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.