Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net database migration, what's the Down method for?

When I add-migration, Up and Down method are generated.

and I know when I update database (update-database), it runs the Up method.

How about Down method?

when it will run, is it for rollback? and, how can I run it?

like image 521
Expert wanna be Avatar asked Dec 26 '22 07:12

Expert wanna be


1 Answers

Its for when you want to "downgrade" the database to a previous migration state. You use it with the -TargetMigration flag of the Update-Database command. For example, if you have added the following migrations:

  • Initial
  • FirstMigration
  • SecondMigration (current state)

You can revert the database to the Initial migration state by:

Update-Database -TargetMigration:Initial

In which case the code in the Down() methods of the SecondMigration and FirstMigration classes will run.

like image 103
jmoerdyk Avatar answered Feb 12 '23 13:02

jmoerdyk