Codefirst Migration Note points

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.