20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx
EF 6 Code-First系列文章目录:
- 1 翻译系列:什么是Code First(EF 6 Code First 系列)
- 2.翻译系列:为EF Code-First设置开发环境(EF 6 Code-First系列)
- 3.翻译系列:EF Code-First 示例(EF 6 Code-First系列)
- 4.翻译系列:EF 6 Code-First默认约定(EF 6 Code-First系列)
- 5.翻译系列:EF 6中数据库的初始化(EF 6 Code-First 系列)
- 6.翻译系列:EF 6 Code-First中数据库初始化策略(EF 6 Code-First系列
- 7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)
- 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
- 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
- 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
- 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
- 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】
- 9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】
- 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
- 10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】
- 10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】
- 11.翻译系列:在EF 6中配置一对零或者一对一的关系【EF 6 Code-First系列】
- 12.翻译系列:EF 6 中配置一对多的关系【EF 6 Code-First系列】
- 13.翻译系列:Code-First方式配置多对多关系【EF 6 Code-First系列】
- 14.翻译系列:从已经存在的数据库中生成上下文类和实体类【EF 6 Code-First系列】
- 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】
- 16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】
- 17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】
- 18.翻译系列:EF 6 Code-First 中的Seed Data(种子数据或原始测试数据)【EF 6 Code-First系列】
- 19.翻译系列:EF 6中定义自定义的约定【EF 6 Code-First约定】
- 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
- 20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】
- 20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】
- 21.翻译系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】
在前面的一节中,你学习了自动迁移技术,当实体改变的时候,自动进行数据库迁移。这里你将会学习基于代码的数据库迁移技术。
基于代码的数据库迁移技术,在迁移的时候,提供了更多的控制。例如允许你配置添加额外的字符串,例如设置列的默认值,配置计算列等等。
为了使用基于代码的数据库迁移,你需要在程序包管理控制台中输入:
- Enable-Migrations:在项目中启用数据库迁移,然后会创建一个Configuration类
- Add-Migration:创建了一个迁移类,其中指定了Up和Down方法。
- Update-Database:执行Add_migration指令中创建的迁移,将改变应用到数据库中。
为了使用基于代码的数据库迁移,首先在程序包管理控制台中执行enable-migrations命令。

Enable-Migrations指令会创建Configuration类,这个Configuration类继承自DbMigrationsConfiguration ,Configuration类中包含这句代码:AutomaticMigrationsEnabled = false.
现在你需要在上下文类中设置数据库初始化策略为MigrateDatabaseToLatestVersion :
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命令创建一个迁移类 ,后面跟着迁移类的名称:

上面的命令将会创建一个时间戳_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
20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】的更多相关文章
- 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/migration-in-code-first.aspx EF 6 Code-First ...
- 12.翻译系列:EF 6 中配置一对多的关系【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-f ...
- Entity Framework入门教程(18)---EF6中基于代码进行配置方式
EF6中基于代码进行配置方式 我们以前对EF进行配置时是在app.config/web.config下的<entityframework>节点下进行配置的,EF6引进了基于代码的配置方法. ...
- 2.EF中 Code-First 方式的数据库迁移
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...
- EF中 Code-First 方式的数据库迁移
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...
- 2017.11.12 web中JDBC 方式访问数据库的技术
JavaWeb------ 第四章 JDBC数据库访问技术 在JavaWeb应用程序中数据库访问是通过Java数据库连接(JavaDateBase Connectivity简称JDBC)数据库的链接一 ...
- [更新中]【South使用总结】django开发中使用South进行数据库迁移
Django开发中使用South进行数据库迁移的使用总结 South的详细资料可产看官方文档http://south.readthedocs.org/en/latest South安装配置 pip i ...
- Android开发中使用代码删除数据库
更多信息参考:Android开发中使用代码删除数据库 在Android开发中,如果用到数据库,就会有一个很麻烦的问题,就是有时候需要删除数据库很麻烦,要打开Android Device Monitor ...
- EF Core 中多次从数据库查询实体数据,DbContext跟踪实体的情况
使用EF Core时,如果多次从数据库中查询一个表的同一行数据,DbContext中跟踪(track)的实体到底有几个呢?我们下面就分情况讨论下. 数据库 首先我们的数据库中有一个Person表,其建 ...
随机推荐
- POJ 1321-棋盘问题【DFS+递归】
题目链接 题目大意: Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大 ...
- Android图片缓存框架Glide
Android图片缓存框架Glide Glide是Google提供的一个组件.它具有获取.解码和展示视频剧照.图片.动画等功能.它提供了灵活的API,帮助开发者将Glide应用在几乎任何网络协议栈中. ...
- javascript中6种基本数据类型详解
javascript中有5中数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number和String,还有一种复杂数据类型——object,object本质是由一组键值 ...
- BZOJ.3351.[IOI2009]Regions(根号分治 差分)
BZOJ 表示非常爽2333 \(Description\) 给定一棵\(n\)个点的树,每个点有一个属性\(1\leq r_i\leq R\). \(Q\)次询问,每次询问给定\(r1,r2\),求 ...
- [HNOI2018]排列
Description: 给定 \(n\) 个整数 \(a_1, a_2, \dots, a_n, 0 \le a_i \le n\),以及 \(n\) 个整数 \(w_1, w_2, \dots, ...
- 文件处理(如果文件存在则追加,不存在则生成多级文件夹以及txt目录)
public static void writeFile(String path,String fileName,String content) throws IOException { File f ...
- Office 2016 for Mac 64位16.14.1(180613)安装包&激活
注意,本页面是属于Mac操作系统使用的Office,如需Windows版的Office请点击下面的传送门. Windows版Office:https://www.itpwd.com/12.html 激 ...
- xcode升级至9.0之后,新建xib报错: Safe Area Layout Guide Before IOS 9.0
直接翻译是:iOS 9.0前安全区域布局指南. 字面意思就是safe area布局应用在iOS 9之前的版本上了. 解决方法: 1,我们可以把safe area勾掉,不用safe area布局,如下图 ...
- hdu 6034 B - Balala Power! 贪心
B - Balala Power! 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6034 题面描述 Talented Mr.Tang has n st ...
- MySql 数据库安装、环境变量配置 以及 本地连接
一.准备 MySql 的安装包,这里我从网上下载的是 mysql-installer-community-5.6.21.1.msi,如下图: 二.,双击 mysql-installer-com ...