title: Insert InitialCreate migration into database without performing the schema changes
body: <p>I am adding Code First migrations to an existing Entity Framework 6 domain with existing databases. I need the following behaviour:</p>
  
  <ol>
  <li>If the database exists, insert the <code>InitialCreate</code> migration but do not perform the contents of the migration <code>Up()</code>.</li>
  <li>If the database does not exist, create it and run the contents of <code>Up()</code> and <code>Down()</code> to create an empty but correct-schema database.</li>
  </ol>
  
  <p>I need (1) for when I release through the Continuous Delivery deployment. I need (2) for when a developer is cleaning down their machine and starting fresh. I do not want to use automatic migrations.</p>
  
  <p>I appreciate that a Migration has no concept of the Database Context, it's only responsible for generating a series of SQL instructions.</p>
  
  <p>Are these my only options?</p>
  
  <p><strong>1. Move contents of Up and Down out of InitialCreate and into Configuration.Seed</strong></p>
  
  <p>The <code>InitialCreate</code> migration runs Up() but no changes are made. In <code>Seed()</code> we have access to <code>DbContext</code>, so we can work out if the tables exist and create them if needed.</p>
  
  <p>I think this might break when there are lots of migrations to run as <code>Seed()</code> is called after the migrations. On an empty database, the creation of the tables would be happening after updates to those schemas.</p>
  
  <p><strong>2. Perform the Up() method as a SQL Script</strong></p>
  
  <p>Migrations allows the developer to put inline SQL into <code>Up()</code> and <code>Down()</code>. Move the creation of the database into inline SQL and add a <code>IF NOT EXISTS</code> at the top.</p>
  
  <p>I don't like this because you lose the use of the model that is supplied with the <code>InitialCreate</code>. If the model is updated, the fixed SQL string won't.</p>
  
  <p><strong>3. Empty out the Up() and Down() methods, do a release, put the creation code back in, do another release</strong></p>
  
  <p>When the <code>InitialCreate</code> migration is run first time, it won't have anything in it. The entry will go into the migrations database without running anything.</p>
  
  <p>Once that first release has been performed, I can then put the creation code back in so that when future developers run it without a database, it will create properly.</p>
  
  <p><em>Note:</em> This one is my current favourite as it uses Entity Framework as designed and I can personally control adding the code back in after a release.</p>
  
  <p><strong>Is there a better way?</strong></p>
  
  <h2>Edit</h2>
  
  <p>I am unable to build the database from empty, this might be something to do with the Context model creation. It uses a bespoke pluggable method:</p>
  
  <pre class="lang-cs prettyprint-override"><code>    public MyObjectContext()
      {
          ((IObjectContextAdapter) this).ObjectContext.ContextOptions.LazyLoadingEnabled = true;
          ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 180;
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          System.Type configType = typeof(AnswerMap);   //any of your configuration classes here
          var typesToRegister = Assembly.GetAssembly(configType).GetTypes()
          .Where(type =&gt; !String.IsNullOrEmpty(type.Namespace))
          .Where(type =&gt; type.BaseType != null &amp;&amp; type.BaseType.IsGenericType &amp;&amp; type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration&lt;&gt;));
          foreach (var type in typesToRegister)
          {
              dynamic configurationInstance = Activator.CreateInstance(type);
              modelBuilder.Configurations.Add(configurationInstance);
          }
          base.OnModelCreating(modelBuilder);
      }
  </code></pre>
  
  <p>All the map objects are in the same DLL, the domain entities used for the database tables are in a separate DLL.</p>