EF automatically updates the database by modifying the model and efmodel automatically updates the database.

Source: Internet
Author: User

EF automatically updates the database by modifying the model and efmodel automatically updates the database.

Recently, when I used MVC + EF to learn how to modify the model and the database was not updated, I found an error on the Internet about automatic database update for data migration. After a long time, I finally got it.

Step 1: In the Package Manager Console: Enable-Migrations-ProjectName the name of the project where EF is located

Step 2: After running, the Migrations folder will be generated in the field. In the Migrations-> Configuration. cs class, set AutomaticMigrationsEnabled to true (that is, the database will be automatically updated when the model is changed)

If a field is deleted, add AutomaticMigrationDataLossAllowed = true (the value of data loss during Automatic migration is acceptable)

Step 3: In OwnDbContext

Public OwnDbContext (): base ("DefaultConnection") {Database. SetInitializer (new MigrateDatabaseToLatestVersion <OwnDbContext, Own. Model. Migrations. Configuration> ());}

PS: there are no third steps in some of the materials to be queried, but I still have to report an error or change it if I don't change it.

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.