I've noticed that when I create a code first database migration using add-migration
it generates a Down()
method as well as an Up()
method.
我注意到,当我使用add-migration创建代码第一次数据库迁移时,它会生成Down()方法和Up()方法。
How do I tell my database to downgrade?
如何告诉我的数据库降级?
2 个解决方案
#1
37
After almost giving up with researching on Google I managed to find this quote from here:
几乎放弃了对Google的研究后,我设法从这里找到了这个引用:
- http://msdn.microsoft.com/en-gb/data/jj591621.aspx#specific
- http://msdn.microsoft.com/en-gb/data/jj591621.aspx#specific
Which Specifies:
其中指定:
Let’s say we want to migrate our database to the state it was in after running our AddBlogUrl migration. We can use the
–TargetMigration
switch to downgrade to this migration.假设我们想要在运行AddBlogUrl迁移后将数据库迁移到它所处的状态。我们可以使用-TargetMigration开关降级到此迁移。
Run the
Update-Database –TargetMigration: AddBlogUrl
command in Package Manager Console. This command will run the Down script for our AddBlogAbstract and AddPostClass migrations.在程序包管理器控制台中运行Update-Database -TargetMigration:AddBlogUrl命令。此命令将为AddBlogAbstract和AddPostClass迁移运行Down脚本。
If you want to roll all the way back to an empty database then you can use the
Update-Database –TargetMigration: $InitialDatabase
command.如果要一直回滚到空数据库,则可以使用Update-Database -TargetMigration:$ InitialDatabase命令。
#2
10
First get the name of the migration that was applied before the one you want to downgrade by issuing the Get-Migrations command.
首先通过发出Get-Migrations命令获取要在降级之前应用的迁移的名称。
PM> Get-Migrations
Retrieving migrations that have been applied to the target database.
201508242303096_Bad_Migration
201508211842590_The_Migration_applied_before_it
201508211440252_And_another
This list shows the migrations listing the most recent applied migration first. Pick the migration that occurs in the list after the one you want to downgrade, ie the one applied before the one you want to downgrade.
此列表显示首先列出最近应用的迁移的迁移。选择要降级的列表之后发生的迁移,即在您要降级的应用之前应用的迁移。
Update-Database –TargetMigration: "<the migration applied before it>"
All migrations applied after the one specified will be down-graded in order starting with the latest migration applied first.
在指定的迁移之后应用的所有迁移将按照从最先应用的最新迁移开始的顺序进行降级。
#1
37
After almost giving up with researching on Google I managed to find this quote from here:
几乎放弃了对Google的研究后,我设法从这里找到了这个引用:
- http://msdn.microsoft.com/en-gb/data/jj591621.aspx#specific
- http://msdn.microsoft.com/en-gb/data/jj591621.aspx#specific
Which Specifies:
其中指定:
Let’s say we want to migrate our database to the state it was in after running our AddBlogUrl migration. We can use the
–TargetMigration
switch to downgrade to this migration.假设我们想要在运行AddBlogUrl迁移后将数据库迁移到它所处的状态。我们可以使用-TargetMigration开关降级到此迁移。
Run the
Update-Database –TargetMigration: AddBlogUrl
command in Package Manager Console. This command will run the Down script for our AddBlogAbstract and AddPostClass migrations.在程序包管理器控制台中运行Update-Database -TargetMigration:AddBlogUrl命令。此命令将为AddBlogAbstract和AddPostClass迁移运行Down脚本。
If you want to roll all the way back to an empty database then you can use the
Update-Database –TargetMigration: $InitialDatabase
command.如果要一直回滚到空数据库,则可以使用Update-Database -TargetMigration:$ InitialDatabase命令。
#2
10
First get the name of the migration that was applied before the one you want to downgrade by issuing the Get-Migrations command.
首先通过发出Get-Migrations命令获取要在降级之前应用的迁移的名称。
PM> Get-Migrations
Retrieving migrations that have been applied to the target database.
201508242303096_Bad_Migration
201508211842590_The_Migration_applied_before_it
201508211440252_And_another
This list shows the migrations listing the most recent applied migration first. Pick the migration that occurs in the list after the one you want to downgrade, ie the one applied before the one you want to downgrade.
此列表显示首先列出最近应用的迁移的迁移。选择要降级的列表之后发生的迁移,即在您要降级的应用之前应用的迁移。
Update-Database –TargetMigration: "<the migration applied before it>"
All migrations applied after the one specified will be down-graded in order starting with the latest migration applied first.
在指定的迁移之后应用的所有迁移将按照从最先应用的最新迁移开始的顺序进行降级。