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.