博客
关于我
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/

你可能感兴趣的文章
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>