原文推荐!点我点我!

添加Migrations文件夹,并生成类文件Configuration.cs

PM> Enable-Migrations -EnableAutomaticMigrations

在Migrations文件夹中新增类文件201309201556388_InitialCreate.cs

Add-Migration InitialCreate   //后面的名字是随便写的

执行结果生成与上面一致的数据库

Update-Database -Verbose

数据库模型中添加City类,执行程序包管理器控制台语句,Migrations文件夹中新增类文件201309201643300_AddCity.cs

Add-Migration AddCity       //后面的名字是随便写的

执行程序包管理器控制台语句,进行数据库版本回溯

Update-Database –TargetMigration:"201309201643300_AddCity"

生成数据库版本之间的Sql脚本

Update-Database -Script -SourceMigration:"201309201643300_AddCity" -TargetMigration:"201309201708043_ModifyCity"

执行Sql脚本

Update-Database -Script

Enable-Migrations指定项目名称

Enable-Migrations -StartUpProjectName Portal

MigrateDatabaseToLatestVersion:将Entity的修改同步到数据库

下面的代码的原文地址

   public class StyleContext:DbContext
{
public StyleContext() : base("name=WMSAidDB")
{
//自动创建表,如果Entity有改到就更新到表结构
Database.SetInitializer<StyleContext>(new MigrateDatabaseToLatestVersion<StyleContext, ReportingDbMigrationsConfiguration>());
} public DbSet<StyleDetail> StyleDetails { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{ } } internal sealed class ReportingDbMigrationsConfiguration : DbMigrationsConfiguration<StyleContext>
{
public ReportingDbMigrationsConfiguration()
{
AutomaticMigrationsEnabled = true;//任何Model Class的修改將會直接更新DB
AutomaticMigrationDataLossAllowed = true;
}
}

最新文章

  1. [OSG][osgEarth]osgEarth例子程序简介
  2. 使用 Fluent API 配置/映射属性和类型(摘自微软Data Access and Storage)
  3. C# Mysql You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ????
  4. 如何优雅的使用vue+vux开发app -03
  5. 创建Oracle数据库
  6. Express 4 更新改变文档
  7. 转 关于C#中派生类调用基类构造函数的理解
  8. Fragment 与 Activity 通信
  9. 在ViewController中View的生命周期
  10. 使用cordova开发app
  11. Mina源码阅读笔记(二)- IoBuffer的封装
  12. phpexcel 使用模板导出报表
  13. HDU 4687 Boke and Tsukkomi (一般图最大匹配)【带花树】
  14. linux基本网络配置
  15. Web开发.net framework 类库中必须掌握的命名空间(或者类)【转】
  16. Intersection Observer API 可以让你知道被观察元素何时进入或退出浏览器的视口
  17. NT1_keras下搭建一个3层模型并且修改。
  18. json和java bean的相互转换(使用fastjson)
  19. 看我如何快速学习.Net(高可用数据采集平台)
  20. 邂逅明下(巴什博弈+hdu2897)

热门文章

  1. Android按键添加和处理的方案【转】
  2. Android和H5交互-基础篇
  3. 5-7 第五天 微信 JS-SDK-简介
  4. selenium3 + python3 - alert定位
  5. log4net实用配置代码
  6. 2015 多校赛 第二场 1006 (hdu 5305)
  7. .Net Core添加分布式Session
  8. Laravel5.1学习笔记5 请求
  9. python--4、装饰器
  10. VC常用代码之创建进程