20.1 翻译系列: EF 6 中自动数据迁移技术 [EF 6 Code-First 系列]
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 系列]
Entity Framework 介绍了自动迁移技术, 所以每次实体发生改变的时候, 你不用手动去处理数据库迁移.
自动迁移技术可以通过在程序包管理控制台中输入并执行: enable-migrations 命令做到. 打开程序包管理控制台, 输入: enable-migrations -EnableAutomaticMigration:$true[确保默认的项目是你现在要执行的项目]
当命令执行成功之后, 将会创建一个 internal sealedConfiguration 类, 这个 Configuration 类继承自 DbMigrationConfiguration :
正如你在 COnfiguration 类的构造函数中看到的那样, AutomaticMigrationsEnabled 被设置为 true.
下一步, 就是在上下文类中设置数据库初始化策略为 MigrateDatabaseToLatestVersion:
- public class SchoolContext: DbContext
- {
- public SchoolDBContext(): base("SchoolDB")
- {
- Database.SetInitializer(new MigrateDatabaseToLatestVersion());
- }
- public DbSet Students { get; set; }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- base.OnModelCreating(modelBuilder);
- }
- }
现在你就完成了自动化迁移技术的配置. 当实体发生改变的时候, EF 将会自动进行数据库迁移. 目前为止, 我们只有一个 Student 实体, 还有一个上下文类, 我们运行项目看看生成的数据库:
你将会发现 EF API 创建了__MigrationHistZ 喎"/kf/ware/vc/" target="_blank" class="keylink">vcnmx7brNU3R1ZGVudHOx7aGjX19NaWdyYXRpb25IaXN0b3J5sPy6rMHLw7+0zsr9vt2/4seo0sa1xMD6yre8x8K8oaM8L3A+CjxwPs/W1NqjrMTjzO2809K7uPbQwrXEwezT8sDgyrXM5aOs1MvQ0LPM0PKjrLvht6LP1sr9vt2/4tfUtq+w/LqswcvL+dPQyrXM5cv507PJ5LXEse2ho8TjsrvTw9TL0NDIzrrOxuTL+8P8we6hozwvcD4KPHA+yLu2+KOs1eLR+da7ysfV67bUzO2808q1zOW78tXf0saz/cq1zOWyxdPQ08OjrLWxxOPP8sq1zOXW0MztvNOhotDeuMS78tXfyb6z/cr00NS1xMqxuvKjrLKisrvG8Nf308Oho8m+s/3B7NPywOC1xMjOus7Su7j2yvTQ1KOs1MvQ0M/uxL+jujwvcD4KPHA+PGltZyBzcmM9"https://www.2cto.com/uploadfile/Collfiles/20190415/20190415123010744.png" alt="\" />
这样是因为你将会丢失相应列的数据. 为了解决这个, 你需要在 Configuration 类的构造函数中, 设置 AutomaticMigrationDataLossAllowed 为 true, 并且设置 AutomaticMigrationsEnabled = true.
为了了解更多 enable-migrations 命令的参数, 可以执行 get-help enable-migrations 或者 get-help enable-migrations -detailed, 你将会看到:
- PM> get-help enable-migrations
- NAME
- Enable-Migrations
- SYNOPSIS
- Enables Code First Migrations in a project.
- SYNTAX
- Enable-Migrations [-ContextTypeName ] [-EnableAutomaticMigrations]
- [-MigrationsDirectory ] [-ProjectName ] [-StartUpProjectName
- ] [-ContextProjectName ] [-ConnectionStringName ]
- [-Force] [-ContextAssemblyName ] [-AppDomainBaseDirectory ]
- []
- Enable-Migrations [-ContextTypeName ] [-EnableAutomaticMigrations]
- [-MigrationsDirectory ] [-ProjectName ] [-StartUpProjectName
- ] [-ContextProjectName ] -ConnectionString
- -ConnectionProviderName [-Force] [-ContextAssemblyName ]
- [-AppDomainBaseDirectory ] []
- DESCRIPTION
- Enables Migrations by scaffolding a migrations configuration class in the project. If the
- target database was created by an initializer, an initial migration will be created (unless
- automatic migrations are enabled via the EnableAutomaticMigrations parameter).
- RELATED LINKS
- REMARKS
- To see the examples, type: "get-help Enable-Migrations -examples".
- For more information, type: "get-help Enable-Migrations -detailed".
- For technical information, type: "get-help Enable-Migrations -full".
来源: https://www.2cto.com/kf/201904/803955.html