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