Reference:
https://msdn.microsoft.com/en-us/data/jj591621
Http://www.itnose.net/detail/6105449.html
Http://www.tuicool.com/articles/Q7JRR32
Open: Tools---NuGet Package Manager--Package management console, use the appropriate commands as follows
Enable migration feature
Command 1. Enable-migrations-contexttypename webtest.models.testdbcontext-migrationsdirectory MigrationsTestDbContext
(Enable-migrations-contexttypename database context name (with fully qualified name, which contains the namespace)-migrationsdirectory folder name)
(This command supports projects that contain multiple database contexts and will create separate folders for each database context)
Add a snapshot of the migration data
Command 2. Add-migration-configuration WebTest.MigrationsTestDbContext.Configuration Initialcreate
(Add-migration-configuration the name of the snapshot to be used (with fully qualified name) (automatically add a timestamp prefix))
(After the model class has changed, use this command to add a migration snapshot, where the first time you use (when the database is not created) is typically named with "Initialcreate")
Update the database to use the migration snapshot for the database
Command 3. Update-database-configuration Webtest.migrationstestdbcontext.configuration-verbose
(update-database-configuration the configuration name to use (with fully qualified name)-verbose (optional, show details, generated SQL statement))
Fallback database
Command 4. Update-database-projectname:webtest-targetmigration: "201706291258382_initialcreate"-verbose
General steps:
1. Set the connection string to create the model class
2. Compiling the project
3. Use command 1 to turn on the migration feature
4. Compiling the project
5. Add an initial snapshot using command 2
6. Compiling the project
7. Using command 3, the database is automatically created, at this point the database initialization is complete
8. Modify the model class when the database needs to be modified when the business is tuned
9. Compiling the project
10. Add a migration snapshot using command 2 (preferably with a meaningful name, such as "addtestbstable")
11. Compiling the project
12. Using command 3 to update the database, the database schema will be upgraded with the original data preserved, keeping the database schema consistent with the latest model
Other
Get help
Get-help enable-migrations
Get the sample
Get-help Enable-migrations-examples
Get help
Get-help Add-migration-full
If present Apply the pending explicit migrations before attempting to generate a new explicit migration. Error, indicating that there is a pending migration operation, you can first comment out the code in the Up () method in the last snapshot (e.g. "201706291123246_initialcreate.cs"), and then run the update-database again. is to do nothing, simply execute the suspended migration.
Apply a database migration to a Web site that has already been published on the server
Tick Execute Code first migrations (runs on application start) as shown in the Web publishing options
EntityFramework Codefirst Database Migration