Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use migration programmatically in EntityFramework Codefirst?

I'm working in a project that uses EF Code First. I'm trying to use migration features. I don't want to use Package Console Manager. How can I execute the "Add-Migration" and "Update-Database" programmatically?

add-migration TestMigration01 -force

update-database
like image 929
ArMaN Avatar asked Feb 02 '16 03:02

ArMaN


People also ask

How do I turn on automatic migration in Entity Framework?

Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations –EnableAutomaticMigration:$true command (make sure that the default project is the project where your context class is).

How do I update an existing database using migration?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.


1 Answers

You have a couple of choices. You can use the dbmigrator class from within your code: http://romiller.com/2012/02/09/running-scripting-migrations-from-code/

Or you can use migrate.exe which is handy for running them in a build step, etc. https://msdn.microsoft.com/en-us/data/jj618307.aspx

like image 144
Steve Greene Avatar answered Sep 28 '22 16:09

Steve Greene