While trying to implement EF Migrations in my project I am stuck at one place.
EF Code First MigrateDatabaseToLatestVersion accepts connection string Name from config.
In my case database name get known at Runtime (User selects it from dropdown). Just the way DbContext either accepts, ConnectionString or connectionString Name in it's constructor, "MigrateDatabaseToLatestVersion" does not accept the same
System.Data.Entity.Database.SetInitializer (new MigrateDatabaseToLatestVersion<SrcDbContext, SRC.DomainModel.ORMapping.Migrations.Configuration>(connString));
Is there any other way to achieve this?
Thank you all. I did checkout the EF code from codeplex, and inherited my own class after understanding their source code. Here is the solution which I opted :-
You can make the MigrateDatabaseToLatestVersion initializer to use the connection string that was used by the context that triggered the migration in the first place. This is done by passing useSuppliedContext: true to the MigrateDatabaseToLatestVersion constructor as described in the docs. In your case:
What context is this running under? Website or Desktop App?
Under website, doing that is not a good idea. The database initializing strategy set against the type of context. So different connection strings with same type of context will override each other's init strategy.
If Desktop App, maybe include an extra utility to switch between database?
Either way I don't recommend doing this, but if you really want to do what you mentioned, it looks like you have to hack it.
The is a issue with Migrations call the DbContext dervied class with parameter. Once this is solved it should work. see here for a sample solution. EntityFramework code-first custom connection string and migrations