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

你可能感兴趣的文章
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>