Automatic migration under EF codefirst

Source: Internet
Author: User

When we modify the data model, add a field like this

Running the program again will cause errors due to inconsistent database structure and model

There are three ways to resolve these errors:

1. Deleting the database, re-running the site, regenerates the database, and the data is lost

2. manually modify the table structure in the database so that it is consistent with the model

3. Use the following tutorial as shown in the EF The Data Migration " Code First Migrations "

in the NuGet The Package Manager executes the following command to enable EF The Data Migration

Two files are generated at this time

where " 201504030707151_initialcreate "is generated from the database files that exist, the code that builds the table, as follows

" Configuration "is the automatically migrated configuration class, Seed method can be used to initialize some data, as follows

we then continue to execute the following command in the Package Manager console to generate the code related to the database churn, Initial You can customize the name of the class that will be generated automatically, but avoid repeating it with other types, or you will get an error

the following generates a " 201504030712220_initial the file

View the code to see that we have added a Description fields:

Finally, we execute the following command in the Package Manager console to write all the changes to the database

at this point, run the site again, you can find that the site has been able to access, Description The field has been successfully added to the database.

Automatic migration under EF codefirst

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.