EF 4.3 Code-Based 数据迁移演练
- 首先第一步:创建一个 MigrationsCodeDemo控制台程序;
- 第二步:添加最新版本EntityFramework NuGet package 到这个项目里:
- Tools –> Library Package Manager –> Package Manager Console.
- Run the ‘Install-Package EntityFramework’ command
- 第三步:添加一个Blog类和一个继承自DbContext的BlogContext:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMigrationsCodeDemo{publicclassBlog{publicintBlogId {get;set; }publicstringName {get;set; }}}
12345678910111213usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.Entity;namespaceMigrationsCodeDemo{publicclassBlogContext : DbContext{publicDbSet<Blog> Blogs {get;set; }}} - 第五步:在控制台入口Program.cs文件里写下如下代码:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMigrationsCodeDemo{classProgram{staticvoidMain(string[] args){using(var db =newBlogContext()){db.Blogs.Add(newBlog { Name ="Another Blog "});db.SaveChanges();foreach(var blogindb.Blogs){Console.WriteLine(blog.Name);}}}}}准备工作做好了,运行这个程序,就就会发现一个名称为 MigrationsCodeDemo.BlogContext数据库就会出现在你的.\SQLEXPRESS 里

- 数据库生成之后不可能就能满足所有以后的需求的,如果我们要更改现有的数据库怎么办?(EF4.3强大的数据迁移功能就体现出来了)
- 我们给现有的blog类添加一个Url 属性试试看
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMigrationsCodeDemo{publicclassBlog{publicintBlogId {get;set; }publicstringName {get;set; }publicstringUrl {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文件
namespaceMigrationsCodeDemo.Migrations{usingSystem.Data.Entity.Migrations;publicpartialclassAddBlogUrl : DbMigration{publicoverridevoidUp(){AddColumn("Blogs","Url", c => c.String());}publicoverridevoidDown(){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)
原文:http://www.cnblogs.com/LittleFeiHu/archive/2012/02/20/2359324.html


浙公网安备 33010602011771号