Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Code-First migration, up/down?

Started to use the add-migration command in the package manager console to generate the migrations for my model. My question is, the up and down method. I assume that the purpose of the down method is to remove all dependencies and drop the tables if they are already in the database? Also that the down method will be executed before the up method? The up method is then the reverse, create/update tables/indexes etc?

Sometimes when i use this then the down method gets a lot of create tables that then are dropped? Recently it created and dropped a lot of tables and almost the same thing happened in the up method. Why?

like image 769
Patrick Avatar asked Mar 19 '12 11:03

Patrick


People also ask

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in coding language?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners. Our C tutorials will guide you to learn C programming one step at a time.


2 Answers

The Up method upgrades your database from its current state (represented by your previous migration) to the state expected by your current code migration. The Down method does the reverse operation - it removes all the changes from the current migration and reverts database to the state expected by the previous migration. It's like installing / uninstalling the migration. Only one of these methods is executed when you call update-database. To use the Down method you must explicitly specify the target migration for your upgrade. If the target migration is the old one, the migration API will automatically use the Down method and downgrade your database.

like image 113
Ladislav Mrnka Avatar answered Oct 01 '22 07:10

Ladislav Mrnka


Just to add to @Ladislav Mrnka. I needed to use Down() for the first time and took me some time to make it work, so:

Update-Database -Target:201407242157114_46 

Where my last migration is 47 (where new stuff was added). Here's a nice explanation of how to rollback the database and remove a bad migration.

Hope it might help other magician apprentices :)

like image 40
HerGiz Avatar answered Oct 01 '22 08:10

HerGiz