LocalDB Database Add new field migration process

Source: Internet
Author: User

You will use the Entity Framework code to migrate some changes to the model class for the first time, so the change applies to the database

1. Click the right mouse button on the Movies.mdf file in the app-data folder and select Delete to delete the movie database.

2. from the Tools menu, click Library Package Manager and then Package Manager console .

Prompt for input in Package Manager console window  PM>

-contexttypename MvcMovie.Models.MovieDBContext then click Enter. Mvcmovie is the name of the project.

3. Create a Configuration.cs file in the new Migration folder Rationsmig.

4 . Open the Configuration.cs file. Replace the method in the Configuration.cs file with Seed方 The following code:

Protected Override void Seed(Mvcmovie.Models.MoviedbcontextContext){Context.Movies.AddOrUpdate(I=I.Title, New Movie { Title = "When Harry Met Sally",//field in table ReleaseDate = Datetime.Parse("1989-1-11"), Genre = "Romantic comedy", Price = 7.99M }, New Movie { Title = "Ghostbusters", ReleaseDate = Datetime.Parse("1984-3-13"), Genre = "Comedy", Price = 8.99M }, New Movie { Title = "Ghostbusters 2", ReleaseDate = Datetime.Parse("1986-2-23"), Genre = "Comedy", Price = 9.99M }, New Movie { Title = "Rio Bravo", ReleaseDate = Datetime.Parse("1959-4-15"), Genre = "Western", Price = 3.99M } );  } 

5, create a dbmigration class for the initial migration. In the Package Manager Console window, enter the command add-migration Initial to create the initial migration. The first migration of the code creates another class file 201508270142453_rating.cs in the Migration folder.
6, Rating property to an existing Movie Class . Open the Models\movie.cs file and add Rating Property:
Movie mode class that is updated in the application is now more than The schema of an existing database for a Movie table is different. Perform the operation again
8, open migrations\ Configuration.cs file and add the rating field to each of the movie's pairs like

9. Build the solution, then open the Package Manager Console window and enter the following command:

add-migration Rating。当此命令完成,Visual Studio 会打开类文件中定义新的DbMIgration 派生类,并在Up 的方法,你可以看到代码,创建新列

10. Build the solution, and then enter the command in the Package Manager Console window update-database . Rerun the program and add the new field successfully.

     

/span>

LocalDB Database Add new field migration process

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.