EvanHua 发表于 2012-12-19 22:28:37

EntityFramework CodeFirst 4.3 step by step (4)

<div id="cnblogs_post_body">昨晚睡不着,想想还是再写点Migration的东西吧,一大早发布到园子里给各位兄弟鉴赏!
上面两篇关于Migration的文章都是介绍如何进行Codebase-Migration的,你肯定要说:烦不烦啊,改点东西就要Package Manager Console里面敲命令。。。
有木有一种自动的Migration方式,在我改好DomainModel后只要敲一次命令就全帮我搞定?这个可以有啊!!!
下面我们介绍Automatic-Migration:
CodeFirst默认的是Codebase-Migration,我们直接把Migrations文件夹中的Configuration类的构造函数中的
AutomaticMigrationsEnabled设为True就OK(不用去敲什么命令了,我敲了敲发现有时候会出一些很无聊的错,你试了就知道,但我觉得没必要试)
假设我们要Blog类中增加User属性,在User类中增加Gender属性,然后一次性更新数据库
<div class="cnblogs_code" >http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gifhttp://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gifBlog<div id="cnblogs_code_open_d4b25bc0-37d6-4882-8203-fb8834d01b67" class="cnblogs_code_hide">1   class Blog2     {3         public int BlogId { get; set; }4         public string Name { get; set; }5         public string Type { get; set; }6         public User User { get; set; }7   }
页: [1]
查看完整版本: EntityFramework CodeFirst 4.3 step by step (4)