Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework and migrations - how to update my database on remote server?

I enabled migrations for my entity framework application. Now, i want to update my database on my remote server. I ran this command:

PM> Update-Database -Script 

So it generated a sql script. But, this script has all the metadata that I actually have in my database, and not the changes that were made; so, when I try to run this sql script on my remote server, it says that tables already exists.

How can i generate sql script, that will contain only necessary updates?

like image 404
ojek Avatar asked Feb 19 '13 17:02

ojek


3 Answers

You can target a specific migration for this. If you have a migration called Foo, for example:

Update-Database -TargetMigration Foo -Script

It will generate a migration script for the Foo migration. Replace Foo with whatever migration you need to run on that server.

Add-Migration InitialMigration
Add-Migration AddCustomers
Add-Migration AddProjects

Let's say you have the above three migrations in your project, and your local database has all of them applied, but the remote database only has InitialMigration. You can run the following:

Update-Database -SourceMigration InitialMigration -TargetMigration AddProjects -Script

This will apply two migrations on the remote server (AddCustomers and AddProjects).

like image 117
Dismissile Avatar answered Nov 15 '22 03:11

Dismissile


If you have access via remote desktop to the server you can execute the command-line migration tool which is usually found in [projectFolder]/packages/EntityFramework.5.0.0/tools/migrate.exe

And call it like this:

Migrate.exe [StartupProjectName] /StartupDirectory:"[BIN folder path]" /ConnectionStringName:"[Connection String Name]" /StartupConfigurationFile:"[Path to web or app.config]"
like image 24
amhed Avatar answered Nov 15 '22 03:11

amhed


You could use the MigrateDatabaseToLatestVersion Initializer to automatically apply any new migrations when you deploy a new version of your application:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, Configuration>());
like image 25
Darren Avatar answered Nov 15 '22 03:11

Darren