Code First Migrations Data migration notes

Source: Internet
Author: User

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

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.