title: Code first custom connection string and migrations without using IDbContextFactory
body: <p>I am trying to write an easy to understand <code>DBContext</code> class that takes a custom connection string, can run migrations, and that I allows me to generate migrations using Package Manager.</p>
  
  <p>I seem to be going around in circles.</p>
  
  <p>I have been able to get it working using code that feels awful to me. I documented this in my answer to <a href="https://stackoverflow.com/questions/15504465/entityframework-code-first-custom-connection-string-and-migrations">This question on connection string and migrations.</a></p>
  
  <p>Radek's answer looks much better than mine, but I find that when I implement it and then try and create a migration in Package Manager I get the message</p>
  
  <blockquote>
    <p>The target context 'DataLayer.Context' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.</p>
  </blockquote>
  
  <p>Where <code>DataLayer.Context</code> is my context class.</p>
  
  <p>I don't want to provide an implementation of <code>IDbContextFactory</code> ( and Radek's answer seems to indicate it isn't needed ) </p>
  
  <p>UPDATE: </p>
  
  <p>I can generate a migration if I include a constructor with no parameter. For example</p>
  
  <pre><code>public Context() : base("ConnectionStringName") { }
  </code></pre>
  
  <p>For my context creation I pass the name of the connection string in app.config</p>
  
  <pre><code>public Context(string connString) : base(connString)
  {
      Database.SetInitializer(new CustomInitializer());
      Database.Initialize(true);
  }
  </code></pre>
  
  <p>At last I am able to both generate migrations, and run migrations for databases that the user selects.</p>
  
  <p>HOWEVER:
  When I drop the database and then run my app I have problems.</p>
  
  <p>The initialiser code I am using, from the link above is </p>
  
  <pre><code>public class CustomInitializer : IDatabaseInitializer&lt;Context&gt;
  {       
      public void InitializeDatabase(Context context)
      {
          try
          {
              if (!context.Database.Exists())
              {
                  context.Database.Create();
              }
              else
              {
                  if (!context.Database.CompatibleWithModel(false))  
                  {
                      var configuration = new Configuration();
                      var migrator = new DbMigrator(configuration);
                      migrator.Configuration.TargetDatabase =
                          new DbConnectionInfo(context.Database.Connection.ConnectionString);
                      IEnumerable&lt;string&gt; migrations = migrator.GetPendingMigrations();
                      foreach (string migration in migrations)
                      {
                          var scriptor = new MigratorScriptingDecorator(migrator);
                          string script = scriptor.ScriptUpdate(null, migration);
                          context.Database.ExecuteSqlCommand(script);
                      }
                  }
              }
          }
          catch (Exception ex)
          {
          }
      }       
  }
  </code></pre>
  
  <p>When I drop the database a new one gets created but it has no tables.  That would be because my table creation code is all in my first migration.</p>
  
  <p>So the code inside the <code>!context.Database.CompatibleWithModel(false)</code> condition does not run.</p>
  
  <p>However alas, the code also does not run the second time around when it should have the metadatamodel. </p>
  
  <p>Now to try and get the migration running...</p>
  
  <p>SADNESS:  No with Radek's custom initializer so far.</p>
  
  <p>From NSGaga's comments I have resorted to exiting the application if I change connection.  </p>
  
  <pre><code>var master = new myMDIForm();
  master.ConnectionType = connectionType;   // being an enum of the different connection names in app.config
  while (master.ConnectionType != ConnectionType.None )
  {
     Application.Run(master);
  }
  </code></pre>