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