2

I may be doing things wrong here, and if that's the case, please point me to the right direction.

I am coding a migration application where I want to be able to type the Connection String I want to use at run time, and be able to Update different databases (potentially each one for a different versions).

The issue for me is that the Add-Migration command only executes through the Package Manager Console, and as I don't have a connection string set under my app.config, how would it be possible for the Entity Migration to know which version my database is at?

I fell into this issue after running the Initial migration for a certain connection string (it created the Database and tables correctly), but after, when I tried to add a second migration file, I got an error saying that there were other migrations pending.

Maybe the Entity Migration just wasn't meant to be executed out of the Package Manager Console, or with dynamic connection strings, but I need to be sure of what I am doing.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
MMalke
  • 1,857
  • 1
  • 24
  • 35

1 Answers1

2

There is already such an application prepared - browse for information on migrate.exe: http://msdn.microsoft.com/en-us/data/jj618307.aspx.

And regarding your question - the ef recognizes at which migration database is very simple. It uses for that __MigrationHistory table within your database. There it has information which migrations was already applied (by MigrationId). The state of the current database and database that should be result after application of the given migration is compared by hash value stored in resx file of your migration under key "Target".

mr100
  • 4,340
  • 2
  • 26
  • 38
  • The point is that I add the migration file through the Package Manager Console, and there is no connection string set under my app.config. It can't check the table __MigrationHistory because the application is not running when I execute the Add-Migration command. I am probably just reinventing the wheel. I'm going to check the link provided and update the answer afterwards. – MMalke Jun 09 '14 at 16:15
  • add-migration and update-database scripts are always executed against some database. – mr100 Jun 09 '14 at 20:14
  • The application does not need to be running so that script could check __MigrationHistory table. – mr100 Jun 09 '14 at 20:50
  • If there was a way to specify which DB I am adding the migration file for, then yes, the command would be able to check the __MH table. Does something like this exist? Thanks for the link, by the way, I've used it while taking some decisions. – MMalke Jun 13 '14 at 13:11
  • 1
    migrate.exe offers you parameters like /connectionString and /connectionProviderName - by passing them as command line arguments you may refer to given db. Alternatively you may refer to your project App.config or Web.config that stores your connection string and pass connection string name. To find out more about available parameters use "migration.exe /?" – mr100 Jun 13 '14 at 18:49
  • Thanks. I believe your help was enough to get me in the right direction. – MMalke Jun 14 '14 at 13:52