1

I have been looking at how to produce a downgrade in EF 4.3.1 Migrations, and all I have found is only about scripting (like this EF 4.3 Migration - how to produce a downgrade script?)

To upgrade my user's production database I call the method MigrateDatabaseToLatestVersion<TContext, TMigrationsConfiguration> upon application startup, so I make sure that all users have the same database schema after they install a new version of my app. I don't need to run any scripts on the client side once the migrations are configured.

What happens if I want to downgrade to a different version? It seems quite obvious that this method only moves Up until it reaches the latest migration... is there something like MigrateDatabaseToVersion<TContext, TMigrationsConfiguration, DbMigration> where the DbMigration object is the target migration?

Can I avoid running a SQL script if I want to downgrade a production database?

Thanks guys!

Community
  • 1
  • 1
Hannish
  • 1,482
  • 1
  • 21
  • 33

1 Answers1

2

You could use the migrate.exe tool which comes with EF to go to specific migrations.

Migrate.exe MyApp.exe /startupConfigurationFile=”MyApp.exe.config” /targetMigration=”myTargetMigration”

Docs can be found at: http://msdn.microsoft.com/en-us/data/jj618307.aspx

Edit: How to use a connection string

Migrate.exe whereYourMigrationsAre.dll /connectionString=”Data Source=localhost;Initial Catalog=blah;whatever else you want to set” /connectionProviderName=”System.Data.SqlClient”
Thewads
  • 4,953
  • 11
  • 55
  • 71
  • Thank you Thewards... I was looking for a solution in code, but it's true that your answer addresses the "no script" part. In your solution, what is the "MyApp.exe.config" file? Care to post an example of the config file? I'll wait a couple of days to see if someone posts a code based solution, in the meantime a vote up for letting me know of the Migrate.exe tool. Thanks! – Hannish Oct 31 '13 at 11:06
  • FYI I need to configure my connection string in code (generated dynamically) so I'm not using the config file for that purpose... that's why I need a code based solution for this. – Hannish Oct 31 '13 at 11:11
  • 1
    You can define the connectionString instead of the configurationFile if you wish. the configurationFile is essentially the file that contains your connection strings. See edit above – Thewads Oct 31 '13 at 11:19
  • Thank you Thewards, I guess I can call the Migrate.exe from my code and pass the dynamic connection string as a parameter, right? – Hannish Oct 31 '13 at 11:40
  • Yea would be your best bet – Thewads Oct 31 '13 at 11:41
  • Ok, then your answer addresses all my questions, kudos! – Hannish Oct 31 '13 at 11:43