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.