Context Constructor
Do not check __migrationhistory cancels the setting to remove the current database from rebuilding the new database when the database model changes.
Database.setinitializer<context> (NULL);
Re-create the database
Database.setinitializer (New dropcreatedatabaseifmodelchanges<portalcontext> ());
Rebuilding database operation, production environment This law is strictly forbidden to use.
Migration Method 1:
automaticmigrationsenabled=true; automatic migration
Enable-migrations Enable migration
Add-migration Adding a migration script for a pending model change
Update-database updating a pending migration to a database
Update-database-verbose update the model to the database and display the update script to copy the update script to run in the production environment
Get-migrations get the migrated that has been applied
Automaticmigrationsenabled=false; non-automatic migration
enable-migrations [-contexttypename <string>] [-enableautomaticmigrations] [-projectname <String>] [- Startupprojectname <string>] [-connectionstringname <string>] [-force] [<commonparameters>]
ContextTypeName: The project inherits the class name from the DbContext.
Enableautomaticmigrations: Turn on automatic migration.
ProjectName: The name of the project that holds the DbContext class.
Startupprojectname: The name of the startup project in the solution that invokes the connection string under the project.
connectionStringName: Connection string name
The above five parameters are necessary to solve the problem, others do not matter.
For example:
Enable-migrations-contexttypename "Mvccodefirst.blogentities"-projectname "Mvccodefirst"-StartUpProjectName " Mvccodefirst "-connectionstringname" Blogentities "-verbose
After filling in order, the problem is resolved.
The same in Add-migration, Update-database also need to fill in the corresponding parameters. Otherwise, the same error will occur.
For example:
Add-migration-name "Editcst_devicephoto"-projectname "Dbaccesslib"-startupprojectname "WebSite"- connectionStringName "Tjssdbcontext"-verbose
Update-database-script-projectname "Dbaccesslib"-startupprojectname "WebSite"-connectionstringname "TJSSDBContext "-verbose
Migration Method 2:
The __migrationhistory table is deleted in the production environment, and the tables in the production environment are updated to match the model.
Codefirst Migration Note points