简体   繁体   English

通过代码向下进行EF 6迁移(不从程序包管理器控制台进行)

[英]Take an EF 6 Migration down via code (not from package manager console)

On an administration page for a portal, we have a button that will migrate the database to the latest version after a code deployment. 在门户网站的管理页面上,我们有一个按钮,它将在代码部署后将数据库迁移到最新版本。 This is done with the SetInitializer (see below) 这是通过SetInitializer完成的(请参见下文)

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MigrationContext, Configuration>());
var dbMigrator = new DbMigrator(new Configuration());
dbMigrator.Update();

For testing purposes, we would like to add another button to down the migration back to the start. 出于测试目的,我们想添加另一个按钮以将迁移向下还原到开始。 In Package Manager Console, this can be with update-database -target 0 . 在Package Manager控制台中,这可以与update-database -target 0

We are using Entity Framework 6, .NET 4.5.1 我们正在使用Entity Framework 6,.NET 4.5.1

Does anyone know of a way to make this happen via code? 有人知道通过代码实现此目标的方法吗?

Thanks 谢谢

Does dbMigrator.Update("PreviousMigrationName"); // Untested 是否执行dbMigrator.Update("PreviousMigrationName"); // Untested dbMigrator.Update("PreviousMigrationName"); // Untested work? dbMigrator.Update("PreviousMigrationName"); // Untested工作?

From https://msdn.microsoft.com/en-us/library/hh829363(v=vs.113).aspx , targetMigration is "The migration to upgrade/downgrade to." https://msdn.microsoft.com/zh-cn/library/hh829363(v=vs.113).aspx中targetMigration是“要升级/降级到的迁移”。

If you want to go down to the first migration, I would think dbMigrator.Update("FirstMigrationName"); 如果您想进行第一次迁移,我认为dbMigrator.Update("FirstMigrationName"); would do the trick. 会成功的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM