- 首先第一步:創建一個 MigrationsCodeDemo控制台程序;
- 第二步:添加最新版本EntityFramework NuGet package 到這個項目里:
- Tools –> Library Package Manager –> Package Manager Console.
- Run the ‘Install-Package EntityFramework’ command
- 第三步:添加一個Blog類和一個繼承自DbContext的BlogContext:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MigrationsCodeDemo { public class Blog { public int BlogId { get; set; } public string Name { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; namespace MigrationsCodeDemo { public class BlogContext : DbContext { public DbSet<Blog> Blogs { get; set; } } }
- 第五步:在控制台入口Program.cs文件里寫下如下代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MigrationsCodeDemo { class Program { static void Main(string[] args) { using (var db = new BlogContext()) { db.Blogs.Add(new Blog { Name = "Another Blog " }); db.SaveChanges(); foreach (var blog in db.Blogs) { Console.WriteLine(blog.Name); } } } } }
准備工作做好了,運行這個程序,就就會發現一個名稱為 MigrationsCodeDemo.BlogContext數據庫就會出現在你的.\SQLEXPRESS 里
- 數據庫生成之后不可能就能滿足所有以后的需求的,如果我們要更改現有的數據庫怎么辦?(EF4.3強大的數據遷移功能就體現出來了)
- 我們給現有的blog類添加一個Url 屬性試試看
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MigrationsCodeDemo { public class Blog { public int BlogId { get; set; } public string Name { get; set; } public string Url { get; set; } } }
如果你就這樣直接再次運行程序的話,肯定會報錯的餓,因為數據庫不再匹配你的Model,
”The model backing the 'BlogContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).”
- 根據錯誤提示,接下來需要使用Code First Migrations to update the database;第一步就是啟用遷移為我們的項目
- Run the ‘Enable-Migrations’ command in Package Manager Console
- Run the ‘Enable-Migrations’ command in Package Manager Console
- 上面的命令執行之后,在你的項目里就會新建一個文件加,文件夾里有兩個文件,分別是Configuration 和InitialCreate migration
- 接下來我們就可以使用EF的Migration來為我們的Blog添加新的屬性並同步到數據庫了,
- Run the ‘Add-Migration AddBlogUrl’ command in Package Manager Console,之后項目的文件夾下就會新建一個AddBlogUrl migration文件
namespace MigrationsCodeDemo.Migrations { using System.Data.Entity.Migrations; public partial class AddBlogUrl : DbMigration { public override void Up() { AddColumn("Blogs", "Url", c => c.String()); } public override void Down() { DropColumn("Blogs", "Url"); } } }
里面主要包括兩個函數,其中up用於數據遷移,down用於數據回溯,當然如果覺得這些操作還不夠可以自己手動修改里面的操作,
- 最后就是Run the ‘Update-Database’ command in Package Manager Console,這樣我們對model的修改就會更新到數據庫里,
- Run the ‘Add-Migration AddBlogUrl’ command in Package Manager Console,之后項目的文件夾下就會新建一個AddBlogUrl migration文件
- 添加完字段之后,我們發現還是原來的設計更好,這時我們就需要回溯我們的數據庫:
Run the ‘Update-Database –TargetMigration:"InitialCreate"’ command in Package Manager Console.,此時數據庫就又回到了剛開始建的是時候,
- 我們給現有的blog類添加一個Url 屬性試試看
- 查看sql:我們在 Package Manager Console執行相關的命令之后,數據庫就做出來相應的改變,如果我們想查看數據遷移過程中這些命令到底為我們做了什么,我們可以使用-script來獲取各個migration的sql,比如我們migrations文件夾下只有兩個migration
我們想查看InitialCreate具體執行了哪些腳本:我們可以Run the ‘Update-Database –TargetMigration:"InitialCreate"’ command in Package Manager Console,
查看AddBlogUrl具體執行了哪些腳本,我們可以Run the ‘Update-Database –TargetMigration:"AddBlogUrl"’ command in Package Manager Console,
一旦這些命令被執行,相關的.sql文件就會被打開在Visual Studio
注意Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you.這是MSDN上的原話,擔心自己翻譯的不准確就沒有翻譯,呵呵,
命令關鍵字總結:
- Install-Package EntityFramework
- Enable-Migrations
- Add-Migration
- Update-Database
- Update-Database –Verbose (相關的腳本會顯示在Package Manager Console里,並最終應用到數據庫)
- Update-Database -Script -SourceMigration:$InitialDatabase -TargetMigration:"AddBlogUrl" (其中AddBlogUrl是Migration Name,這是生成sql文件but instead of actually applying the changes)
看了.Net開發人員可以擁抱Entity Framework 了(EF4.3 Release!!!)之后結合MSDN寫下的學習筆記,多多指教哦