博客
关于我
20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】
阅读量:416 次
发布时间:2019-03-06

本文共 2552 字,大约阅读时间需要 8 分钟。

原文链接:

 

EF 6 Code-First系列文章目录:

 

  • )
  • )
  • )
  •        
  •        
  •        
  •        
  •        
  •        
  •        
  •        
  •        
  •        
  •        
  •        
  •        
  •        
  •        
  •        

 

Entity Framework介绍了自动迁移技术,所以每次实体发生改变的时候,你不用手动去处理数据库迁移。

自动迁移技术可以通过在程序包管理控制台中输入并执行:enable-migrations命令做到。打开程序包管理控制台,输入:enable-migrations –EnableAutomaticMigration:$true【确保默认的项目是你现在要执行的项目】

当命令执行成功之后,将会创建一个internal sealed Configuration 类,这个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创建了__MigrationHistory 表和Students表。__MigrationHistory 包含了每次数据库迁移的历史记录。

现在,你添加一个新的领域类实体,运行程序,会发现数据库自动包含了所有实体所映射的表。你不用运行任何其他命令。

然而,这样只是针对添加实体或者移除实体才有用,当你向实体中添加、修改或者删除属性的时候,并不起作用。删除领域类的任何一个属性,运行项目:

这样是因为你将会丢失相应列的数据。为了解决这个,你需要在Configuration类的构造函数中,设置AutomaticMigrationDataLossAllowed 为true,并且设置AutomaticMigrationsEnabled = true。

为了了解更多enable-migrations命令的参数,可以执行get-help enable-migrations 或者get-help enable-migrations -detailed,你将会看到:

PM> get-help enable-migrationsNAME    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 LINKSREMARKS 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".

 

 

转载地址:http://fvxkz.baihongyu.com/

你可能感兴趣的文章
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>
Mysql InnoDB 数据更新导致锁表
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>