原文連接:https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspxhtml
EF 6 Code-First系列文章目錄:數據庫
在前面的一節中,你學習了自動遷移技術,當實體改變的時候,自動進行數據庫遷移。這裏你將會學習基於代碼的數據庫遷移技術。app
基於代碼的數據庫遷移技術,在遷移的時候,提供了更多的控制。例如容許你配置添加額外的字符串,例如設置列的默認值,配置計算列等等。ide
爲了使用基於代碼的數據庫遷移,你須要在程序包管理控制檯中輸入:學習
爲了使用基於代碼的數據庫遷移,首先在程序包管理控制檯中執行enable-migrations命令。測試
Enable-Migrations指令會建立Configuration類,這個Configuration類繼承自DbMigrationsConfiguration
,Configuration類中包含這句代碼:AutomaticMigrationsEnabled = false
.ui
如今你須要在上下文類中設置數據庫初始化策略爲MigrateDatabaseToLatestVersion
:spa
public class SchoolContext: DbContext { public SchoolDBContext(): base("SchoolDB") { Database.SetInitializer(new MigrateDatabaseToLatestVersion<SchoolDBContext, EF6Console.Migrations.Configuration>()); } public DbSet<Student> Students { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { } }
如今使用Add-Migration命令建立一個遷移類 ,後面跟着遷移類的名稱:.net
上面的命令將會建立一個時間戳_SchoolDB-v1.cs文件,類裏面包含Up和Down方法:翻譯
正如你所見,Up方法包含建立數據庫對象的代碼,而且Down方法包含刪除數據庫的代碼。你一樣能夠編寫代碼,進行額外的配置。這就是優於自動遷移的地方。
爲了瞭解更多add-migrations命令參數,你能夠執行get-help add-migration或者get-help add-migration -detailed:
PM> get-help add-migration NAME Add-Migration SYNOPSIS Scaffolds a migration script for any pending model changes. SYNTAX Add-Migration [-Name] <String> [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] [-ConnectionStringName <String>] [-IgnoreChanges] [-AppDomainBaseDirectory <String>] [<CommonParameters>] Add-Migration [-Name] <String> [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] -ConnectionString <String> -ConnectionProviderName <String> [-IgnoreChanges] [-AppDomainBaseDirectory <String>] [<CommonParameters>] DESCRIPTION Scaffolds a new migration script and adds it to the project. RELATED LINKS REMARKS To see the examples, type: "get-help Add-Migration -examples". For more information, type: "get-help Add-Migration -detailed". For technical information, type: "get-help Add-Migration -full".
在使用Add-Migration命令以後,你須要更新數據庫。經過執行Update-Database命令,來提交修改到數據庫中,還能夠在後面加上–verbose 就能夠看到生成的SQL腳本:
執行get-help update-database或者get-help update-database -detailed命令:
PM> get-help update-database NAME Update-Database SYNOPSIS Applies any pending migrations to the database. SYNTAX Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] [-ConnectionStringName <String>] [-AppDomainBaseDirectory <String>] [<CommonParameters>] Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] -ConnectionString <String> -ConnectionProviderName <String> [-AppDomainBaseDirectory <String>] [<CommonParameters>] DESCRIPTION Updates the database to the current model by applying pending migrations. RELATED LINKS REMARKS To see the examples, type: "get-help Update-Database -examples". For more information, type: "get-help Update-Database -detailed". For technical information, type: "get-help Update-Database -full".
到這個時候,數據庫就被建立或更新了,如今無論何時,模型發生改變的時候,執行Add-Migration 帶上參數名,就建立一個新的遷移文件,而後執行Update-Database命令,就將修改提交到數據庫了。
遷移回退
假設你想要回退到以前的任何一個狀態,那麼你能夠執行update-database後面跟着–TargetMigration,指定你想要回退的版本。例如,假設SchoolDB數據庫有不少遷移記錄,可是你想回退到第一個版本,那麼你能夠執行下面的代碼:
PM> update-database -TargetMigration:SchoolDB-v1