内容简介: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.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Algorithm Design
Jon Kleinberg、Éva Tardos / Addison-Wesley / 2005-3-26 / USD 144.20
Algorithm Design introduces algorithms by looking at the real-world problems that motivate them. The book teaches students a range of design and analysis techniques for problems that arise in compu......一起来看看 《Algorithm Design》 这本书的介绍吧!