title: How does Database.SetInitializer actually work? (EF code-first create database and apply migrations using several connection strings)
body: <p>I am trying to write a method to create a database and run migrations on it, given the connection string.  </p>
  
  <p>I need the multiple connections because I record an audit log in a separate database.
  I get the connection strings out of app.config using code like </p>
  
  <pre><code>ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
  </code></pre>
  
  <p>The code works with the first connection string defined in my app.config but not others, which leads me to think that somehow it is getting the connection string from app.config in some manner I don't know.</p>
  
  <p>My code to create the database if it does not exist is</p>
  
  <pre><code>private static Context MyCreateContext(string ConnectionString)
    {
     // put the connection string where the factory method can get it
     AppDomain.CurrentDomain.SetData("ConnectionString", ConnectionString );
     var factory = new ContextFactory();
     // I know I need this line - but I cant see how what follows actually uses it
     Database.SetInitializer(new MigrateDatabaseToLatestVersion&lt;Context,DataLayer.Migrations.Configuration&gt;());
     var context = factory.Create();
     context.Database.CreateIfNotExists(); 
     return context
     }
  </code></pre>
  
  <p>The code in the Migrations.Configuration is </p>
  
  <pre><code>Public sealed class Configuration :  DbMigrationsConfiguration&lt;DataLayer.Context&gt;
  {
      public Configuration()
      {
          AutomaticMigrationsEnabled = false;
      }
  }
  </code></pre>
  
  <p>The context factory code is </p>
  
  <pre><code>  public class ContextFactory : IDbContextFactory&lt;Context&gt;
  {
      public Context Create()
      {
          var s = (string)AppDomain.CurrentDomain.GetData("ConnectionString");
  
          return new Context(s);
      }
  }
  </code></pre>
  
  <p>Thus I am setting the connection string before creating the context.<br>
  Where can I be going wrong, given that the connection strings are all the same except the database name, and the migration code runs with one connection string, but doesnt run with others?</p>
  
  <p>I wonder if my problem is to do with understanding how How does Database.SetInitializer actually works. I am guessing something about reflection or generics. How do i make the call to SetInitializer tie  tie to my actual context?  </p>
  
  <p>I have tried the following code but the migrations do not run</p>
  
  <pre><code> private static Context MyCreateContext(string ConnectionString)
      {
          Database.SetInitializer(new MigrateDatabaseToLatestVersion&lt;Context, DataLayer.Migrations.Configuration&gt;());
          var context = new Context(ConnectionString);
          context.Database.CreateIfNotExists();
      }
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/15504465/entityframework-code-first-custom-connection-string-and-migrations">This question appears to be related</a></p>
  
  <p>UPDATE:</p>
  
  <p>I can get the migrations working if I refer to the connection string using 
       public MyContext() : base("MyContextConnection") - which points to  in the config</p>
  
  <p>I was also able to get migrations working on using different instances of the context, if I created a ContextFactory class and passed the connection to it by referencing a global.  ( See my answer to the related question link )</p>
  
  <p>Now I am wondering why it has to be so hard.</p>