内容简介: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()
都会去检测是否有待迁移内容,有的话,自动应用迁移。
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和之前的库存在冲突,要注意删库让它自动重建,否则会报错。
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.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 银行核心海量数据无损迁移:TDSQL数据库多源异构迁移方案
- 再无需从头训练迁移学习模型!亚马逊开源迁移学习数据库 Xfer
- Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
- Spring Cloud Alibaba迁移指南1:零代码从Eureka迁移到Nacos 原 荐
- Spring Cloud Alibaba迁移指南2:一行代码从Hystrix迁移到Sentinel 原 荐
- 数据迁移的套路
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Android 源码设计模式解析与实战
何红辉、关爱民 / 人民邮电出版社 / 2015-11 / 79.00元
本书专门介绍Android源代码的设计模式,共26章,主要讲解面向对象的六大原则、主流的设计模式以及MVC和MVP模式。主要内容为:优化代码的首步、开闭原则、里氏替换原则、依赖倒置原则、接口隔离原则、迪米特原则、单例模式、Builder模式、原型模式、工厂方法模式、抽象工厂模式、策略模式、状态模式、责任链模式、解释器模式、命令模式、观察者模式、备忘录模式、迭代器模式、模板方法模式、访问者模式、中介......一起来看看 《Android 源码设计模式解析与实战》 这本书的介绍吧!