降级(回滚)数据库,在生产环境中以代码优先 [英] Downgrade (Rollback) Database with code-first in production environment
问题描述
我有一个客户与应用程序 - 版本1.现在我要升级到版本2.我想启用升级应用程序的数据库(在生产文件中,使用CMD安装从一个包),但有可能降级数据库,如果会有任何问题 - 但不删除现有行。
例如 - 如果我有一个表项目和项目有键,名称,位置
。升级时,我添加一个新列:电子邮件
。降级 - 新列将被删除。
我在Visual Studio上创建了迁移我得到这个代码(只是为了示例 - 我有更多的迁移):
public partial class AddEmail:DbMigration
{
public override void Up()
{
AddColumn(dbo.Items,Email,c => c。串());
}
public override void Down()
{
DropColumn(dbo.Items,Email);
}
}
现在,我在现有的版本,它工作正常 - 新列已添加,它与新代码一起工作。我添加了几个项目(行)。
现在,如何重新安装旧版本,以便新列被删除?实际上我想回滚新的迁移 - 但是我不想丢失新的行,只有新列。
在这篇文章中,有一个解释如何创建脚本从一个迁移迁移到另一个迁移 - 升级或降级。
所以我运行在包管理器控制台 -
更新数据库-Script -TargetMigration:MyMigration1
并获得SQL脚本。
我需要通过它修复一下,然后我刚刚在生产数据库中运行它来降级数据库。
I have a web application that I install on my customers' computers for their inner use. I use C# MVC5 and code-first Entity Framework. I used automatic migration=true but I stopped and set it to false. I installed it on a production environment (release) - with a deploy-package (no Visual Studio).
I have a customer with the application - version 1. Now I want to upgrade to version 2. I want to enable to upgrade the App's DB (in production file, with CMD installation from a package), but to have the possibility to downgrade the DB if there'll be any problems - but without deleting the existing rows.
For example - if I have a table "Items" and Items has Key, Name, Location
. On upgrade I add a new column: Email
. On downgrade - the new column will be deleted.
I created the migration on Visual Studio I get this code (It's just for the example - I have more migrations):
public partial class AddEmail : DbMigration
{
public override void Up()
{
AddColumn("dbo.Items", "Email", c => c.String());
}
public override void Down()
{
DropColumn("dbo.Items", "Email");
}
}
Now, I installed the App's new version on the existing version and it worked fine - the new column was added and it worked with the new code. I added a few items (rows).
Now, how can I re-install the old version so the new column will be deleted? Actually I want to rollback the new migrations - but I don't want to lose the new rows, only the new columns.
In this article there is an explanation how to create a script that migrate from one migration to another - upgrade or downgrade. So I ran in the Package Manager Console -
Update-Database -Script -TargetMigration: MyMigration1
and got the SQL script.
I needed to go through it a fix it a little and then I just ran it on the Production Database to downgrade the DB.
这篇关于降级(回滚)数据库,在生产环境中以代码优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!