Summary of EF Data migration issues

Source: Internet
Author: User

When using the Entity Framework's code first mode in a project, when data migration occurs, the Migration folder is the way in which each entity's modifications are synchronized to the data, with only the up and down methods in each file. For example, for the first data migration, a data table is created and the command is entered in the Package Management console: Add-migration Migrationname, which generates a Migrationname.cs file, the code for the corresponding migration class file is as follows:

1  Public Override voidUp ()2         {3 CreateTable (4                 "dbo. Fileboxes",5c =New6                     {7Id = C.long (nullable:false, Identity:true),8Fileno =c.string (),9Filetitle =c.string (),TenVersion =c.string (), OneFileType =c.string (), ABoxno =c.string (), -Managestyle =c.string (), -Year =c.string (), theRemark =c.string (), -                     }) -. PrimaryKey (t =t.id); -              +         } -          +          Public Override voidDown () A         { atDroptable ("dbo. Fileboxes"); -}

You can synchronize the design of the entity to the database by entering the command in the Package Management console: Update-database.

When you modify the entity later, run Add-migration Migrationname and update-database again to regenerate a synchronization file and update the changes to the database. For example, to change the year attribute in entity to years, the resulting synchronization file code is as follows:

 Public Override voidUp () {AddColumn ("dbo. Fileboxes","years", C =c.string ()); Dropcolumn ("dbo. Fileboxes"," Year"); }                 Public Override voidDown () {AddColumn ("dbo. Fileboxes"," Year", C =c.string ()); Dropcolumn ("dbo. Fileboxes","years"); }

Here are two questions to note:

1, can not arbitrarily delete the migration folder in the synchronization files, or data synchronization will fail. I have several migration in the project, but will be the first time to create a data table Miragtion file deleted, always update-database failure, find long time only to find the reason here.

2, each time the Add-migration file name can not be duplicate. Name can be the date of the day to join the version number of the day, for example: 20160514001,20160514002.

Summary of EF Data migration issues

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.