Failure (rollback) database with code one in production

I have a web application that I install on my clients' computers for internal use. I am using C # MVC5 and Code Entity Framework. I used automatic migration = true, but I stopped and set it to false. I installed it in a production environment (release) - with the deploy-package (without Visual Studio).

I have a client with the application - version 1. Now I want to switch to version 2. I want to enable updating the application database (in a working file, installing CMD from the package), but in order to be able to lower the database if there are any problems, but without deleting existing rows.

For example, if I have a table called β€œElements” and the elements have Key, Name, Location . When updating, I add a new column: Email . When downgraded, the new column will be deleted. I created a migration in Visual Studio. I get this code (this is just for 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 new version of the application into the existing version, and it worked fine - a new column was added and it worked with the new code. I have added a few elements (rows).

Now, how can I reinstall the old version so that the new column is deleted? Actually I want to undo new migrations, but I do not want to lose new rows, only new columns.

+7
c # ef-code-first code-first-migrations downgrade
source share
2 answers

This article explains how to create scripts that migrate from one migration to another β€” upgrade or downgrade. So I launched the package manager console -

Update-Database -Script -TargetMigration: MyMigration1

and got the SQL script.

I needed to go through this a little fix, and then I just ran it in the Production database to downgrade the database.

+13
source share

You can proceed to a specific migration using this technique: https://msdn.microsoft.com/en-us/data/jj591621.aspx#specific

Keep in mind that target migration is required, so you must set up a basic snapshot of your database using Add-Migration Initial -IgnoreChanges (see https://msdn.microsoft.com/en-us/data/dn481501.aspx ) otherwise use Update-Database -TargetMigration: 0

When you roll it back, it will be interrupted if there is data in the columns, so you may need to update the -Force database

0
source share

All Articles