Code First Data migration go

Source: Internet
Author: User

I. Setting up Code First Data migration for model changes

1, tools---Library Package Manager, Package Manager console, input "Enable-migrations" or

Enable-migrations-contexttypename Mvc.Models.DataContext

Enable-migrations-contexttypename namespace. Context Name

Note: If correct, the Code first migration has been enabled for Project XXX. ”,

Enable-migrations command creates a migrations folder and Configuration.cs file

2, the Package Manager console, enter "Add-migration Initial" to create an initial migration.

Note: The name "Initial" is arbitrarily named, that is, the field to be added. Used to name the created migration file.

3, Package Manager console, input "Update-database"
4, run the program, update completed.

Second, further
The problem has been solved, so let's take a closer look at this migration approach.
When we add columns (adding entity fields), you don't have to repeat all of the above actions.
1. Only the automaticmigrationsenabled = False in the \migrations\configuration.cs file should be changed to True
2, tools, library Package Manager, Package Manager console, input "Update-database"

Third, when we delete the column (delete entity field), also use the above operation, no success, the report will lose data,

So you can't simply execute "update-database" to complete.

1, execute the command "Add-migration Initial"
2. Execute command "update database"
3, run the program, update completed

Four, very powerful migration

public class Contactcontext:dbcontext
{
Public Contactcontext ()
: Base ("Name=defaultconnection")
{
Automatically create tables if entity has changed to update to table structure
Database.setinitializer<contactcontext> (New Migratedatabasetolatestversion<contactcontext, Reportingdbmigrationsconfiguration> ());
}


protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)
{
Modelbuilder.conventions.remove<pluralizingtablenameconvention> (); The table name is the class name, not the name above with S//the contract that removes the plural table name
Modelbuilder.conventions.remove<includemetadataconvention> (); Do not create edmmetadata table//Prevent shady transactions or visit edmmetadata this table every time
}


Public dbset<city> Cities {get; set;}
Public dbset<area> area {get; set;}
Public dbset<campaign> Campaign {get; set;}
}


Internal sealed class Reportingdbmigrationsconfiguration:dbmigrationsconfiguration<contactcontext>
{
Public Reportingdbmigrationsconfiguration ()
{
automaticmigrationsenabled = true;//changes to any model class will update the DB directly
Automaticmigrationdatalossallowed = true;
}
}


Reference URL

Http://www.cnblogs.com/hugh251325/archive/2013/03/21/2972431.html

Http://www.cnblogs.com/youring2/p/mvc-5-adding-a-new-field.html

http://blog.csdn.net/zy332719794/article/details/8845405

Http://www.ejutuo.com/bbs/thread-3225-1-1.html

Some tips for code first programming in http://www.cnblogs.com/chinabc/archive/2013/01/16/2846062.html MVC

Code First Data migration go

Related Article

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.