I'm not sure exactly what problems you are facing, but let me try
The easiest way to connect is to make sure it works that way ...
1) Use your 'DbContext' class name - and define the connection in app.config (or web.config). This is easiest, you should have a connection matching your context class name,
2) If you put it in DbContext through the constructor - then be consistent and use it. I also suggest “reading” from configuration connections and again calling it “the same” as your context class (use the name “name” rather than the actual string),
3) if none are present - EF / CF does the "default" - based on your provider - and your context class name - which is usually not what you want,
You should not configure using initializers for this reason - initializers should be agnostic and serve other purposes - configure the connection in .config - or directly on DbContext
Also check out this Entity Framework Code First - How do I tell my application that NOW uses the production database after development is complete instead of creating a local db?
Always check where your data goes - before doing anything.
For how the initializer really works , check out this other post, I made a detailed example
How to create an initializer to create and migrate a mysql database?
Notes: (from comments)
The connection does not have to be very dynamic - config is the right place for it unless you have a good reason.
The constructor also works great.
CreateDbIfNotExists does not work with the migration initializer. You can simply use the MigrateDatabaseToLatestVersion initializer. Don't mix it
Or - put something like public MyContext() : base("MyContextConnection") - which points to <connectionStrings> in config
To indicate a connection, simply use its "name" and put it in the constructor.
Or use somehting as ConfigurationManager.ConnectionStrings["CommentsContext"].ConnectionString
Concerning entertaining "multiple databases" with migrations (local and remote from one application) - not quite connected - but this link - Migration does not work as I wish ... Asp.net EntityFramework
Update: (further discussion here - Is adding a class that inherits from something a violation of solid principles if it changes the behavior of the code? )
It is interesting here. I was able to reproduce the problems you are facing. Here is a brief breakdown of what I think is happening:
Firstly, it worked “happily”:
Database.SetInitializer(new CreateAndMigrateDatabaseInitializer<MyContext, MyProject.Migrations.Configuration>()); for (var flip = false; true; flip = !flip) { using (var db = new MyContext(flip ? "Name=MyContext" : "Name=OtherContext")) {
(I used a custom initializer from my other post, which controls the migration / creation "manually")
This worked great without an Initializer. As soon as I turned it on, I ran into some interesting problems.
I deleted Db-s (two, for each connection). I expected that either it would not work or it would create one db and then another in the next pass (for example, without migration, just the “Create” initializer).
What happened, to my surprise, did it actually create both databases on the first pass ??
Then, being a curious person :), I set breakpoints on MyContext ctor and debugged through migrator / initializer. Again empty / no db-s, etc.
He created the first instance of my call in flip . Then, at the first access to the "model", he called the initializer. The migrator occupied (did not have dB). During migrator.Update(); it actually creates MyContext (I guess through the general parameter in Configuration) - and calls the "default" empty ctor. This had a "different connection / name" by default, as well as all other Db.
So, I think it explains what you are experiencing. And why did you need to create a "Factory" to support the creation of Context. This seems to be the only way. And by setting some "AppDomain" connection string (which you really actually did), which by default is not canceled by the ctor call.
The solution I see - you just need to run everything through the factory - and have the 'flip' connections there (no need for a static connection if your factory is single.