The Entity Framework Code First database is automatically updated.

Source: Internet
Author: User

The Code First method of EF allows you to write the Model First, and then generate the database and table through the Model.

The procedure is as follows:

1. Create a project

2. In the model folder, add a class derived from DbContext and some Model classes.

3. Modify the connection string in web. Config.

4. Generate the baseline Controller.

In this way, you can generate databases and tables.

However, in actual development, it is inevitable that the model and the derived Context will be modified. An exception will occur when the model is run again, prompting that the model supporting the "MyContext" Context has been changed after the database is created. Please consider using Code First to migrate and update the database "exception.

You can follow these steps to update the database to eliminate the preceding exceptions:

1. In the "package management console" Window of the project, enter "enable-migrations" and press Enter. As a result, a "Migrations" folder is generated in the project, which contains two. cs classes.

2. In Configuration. cs generated in 1, modify the constructor code to: "AutomaticMigrationsEnabled = true;"

3. After saving the changes, enter "update-database" in the "package management console", Press enter, and run again.

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.