A. command to open
1. Open the console: view, other windows, Package Manager console;
2. Start the database migration, execute the command: enable-migrations
After successful creation, the Migrations directory is added.
If the following error is reported:
pm> enable-migrations
More than one context type is found in the assembly ' Firstmvc '.
To enable migrations for FirstMVC.Models.UsersContext, use Enable-migrations-contexttypename FirstMVC.Models.UsersContext.
To enable migrations for FirstMVC.Models.FirstMVCContext, use Enable-migrations-contexttypename FirstMVC.Models.FirstMVCContext.
To specify a specific data context class.
I have only one database, can not input.
3. Run the Database migration
After you start the database migration settings successfully, you need to manually create a database migration to modify the model structure.
* * Each subsequent modification of the model interface, the following 2 commands are executed. **
The command is as follows:
(1). add-migration [Custom version name]
(2). update-database
pm> add-migration Updateorderheader
Scaffolding is being built for migrating "Updateorderheader".
The designer code for this migration file contains a snapshot of the current Code first model. This snapshot is used to calculate changes to the model the next time the migration scaffolding is built. If you make other changes to the model that you want to include in this migration, you can re-build the scaffolding by running Add-migration 201407020938316_updateorderheader again.
pm> update-database
Specify the "-verbose" tag to view the SQL statements applied to the target database.
Applying code-based migration: [201407020938316_updateorderheader].
Applying code-based migration: 201407020938316_updateorderheader.
The Seed method is running.
Pm>
4. Modify the Code configuration
When the above steps are completed,
(1). In the generated catalog file Migrations/configuration.cs
Set automaticmigrationsenabled = false; Modified to automaticmigrationsenabled = true;
(2). Add the following line of code to the Application_Start of the project Global.asax:
System.Data.Entity.Database.SetInitializer (New system.data.entity.migratedatabasetolatestversion< Firstmvc.models.firstmvccontext,migrations.configuration> ());
notes: FirstMVC.Models.FirstMVCContext The data context class to create.
migrations.configuration for successful execution enable-migrations the file class that is generated after the command.
OK, after each modified model only need to perform the above 3rd step 2 commands.
ASP. NET MVC 4 under Code first database migration