After using the Codefirst, a big problem is the synchronization of the attribute fields in the code with the tables in the database, the deletion of the database regeneration can be solved, but the data is lost (of course, by initializing the database in the code to add data can also be resolved, the initialization of the task can be achieved by overriding the seed method). There are a lot of situations where we want the test data to be known, by querying the code first migrations this scenario.
There are a lot of things about code first migrations on the web, and here are just a few of the things I've encountered.
PS: Development environment vs2013 Rtm+sqlserver 2008+ef4.4
1.pm> update-database times wrong: Could not load file Microsoft.VisualStudio.Shell 10
Steps to resolve:
A. Install Microsoft.VisualStudio.Shell 10, the problem remains
B.MSDN Forum on the official Microsoft Solution is to install vs2013 Update2, I installed the update4, the problem remains.
C.stackoverflow on the recommended installation Microsoft.VisualStudio.Shell 12, after installation to solve! This solution to people can not touch the mind, reported the error is SHELL10 error, unexpectedly installed shell12 to solve.
Links: https://www.microsoft.com/en-us/download/details.aspx?id=30670
2.pm> Enable-migrations-force,force parameter is the meaning of forced modification, do not add this parameter will receive a warning of lost data
3. Set automaticmigrationsenabled = True in the generated Confifuration.cs file; --Database schema synchronization
4.pm> add-migration Addcarddataaddtimemig,addcarddataaddtimemig is the name of the generated class, defined by itself. I'm here to add a carddataaddtime field to the consumes table with the type string
Then modify the code:
Public Override voidUp () {AddColumn ("dbo. Consumes","Carddataaddtime", C =c.string ());} Public Override voidDown () {Dropcolumn ("dbo. Consumes","Carddataaddtime");}
PS: If you add a table, the CreateTable code snippet will be generated automatically
5.pm> Update-database If there is no error, the database has been updated after execution.
Code First Migrations Data migration notes