EFCore自动迁移

栏目: ASP.NET · 发布时间: 6年前

内容简介:Gets all migrations that are defined in the assembly but haven't been applied to the target database.

EFCore自动迁移

2019/05/14,EFCore 2.2.4

有两种方式:

1.使用Migrate()方法

if (DbContext.Database.GetPendingMigrations().Any())
{
    DbContext.Database.Migrate(); //执行迁移
}

Migrate() 方法使用前需在程序包管理控制台执行 Add-migration 迁移命令。之后程序每次启动, GetPendingMigrations() 都会去检测是否有待迁移内容,有的话,自动应用迁移。

GetPendingMigrations方法官方文档说明

Gets all migrations that are defined in the assembly but haven't been applied to the target database.

2.使用EnsureCreated()方法

//如果成功创建了数据库,则返回true
DbContext.Database.EnsureCreated()

此方法不需要先执行 Add-migration 迁移命令,如果数据库不存在,则自动创建并返回 true

如果已经创建了数据库后,又改动了实体Model和之前的库存在冲突,要注意删库让它自动重建,否则会报错。

EnsureCreated方法官方文档说明

Ensures that the database for the context exists. If it exists, no action is taken. If it does not exist then the database and all its schema are created. If the database exists, then no effort is made to ensure it is compatible with the model for this context.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

反应式设计模式

反应式设计模式

Roland Kuhn、Brian Hanafee、Jamie Allen / 何品、邱嘉和、王石冲、林炜翔审校 / 清华大学出版社 / 2019-1-1 / 98.00 元

《反应式设计模式》介绍反应式应用程序设计的原则、模式和经典实践,讲述如何用断路器模式将运行缓慢的组件与其他组件隔开、如何用事务序列(Saga)模式实现多阶段事务以及如何通过分片模式来划分数据集,分析如何保持源代码的可读性以及系统的可测试性(即使在存在许多潜在交互和失败点的情况下)。 主要内容 ? “反应式宣言”指南 ? 流量控制、有界一致性、容错等模式 ? 得之不易的关于“什么行不通”的经验 ? ......一起来看看 《反应式设计模式》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具