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?
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).
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]"
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>());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With