title: Entity framework code first migration strategy with existing database
body: <p>I have the following situation and unable to determine correct migration strategy. Help is appreciate.</p>
  
  <ul>
  <li>Application creates and uses database as data storage </li>
  <li>Application needs to update database on the start up <strong>if needed</strong></li>
  <li>using <strong>Nuget Manager console</strong> is not a option. (For migration purpose , locally no problem)</li>
  <li>I Have existing database in distribution which are not EF</li>
  </ul>
  
  <p>Now I want to start using the EF code first approach. What I need to achieve is :</p>
  
  <ol>
  <li>If no database then create one </li>
  <li>If database exists use empty migration (just to be ready for the next upgrades)</li>
  <li>This should happened on application start</li>
  </ol>
  
  <p>Database don't exists ====> Create EF Initial =====> Upg v1 =====> Upg V2 </p>
  
  <p>Database Exists =====> Skip Initial but be ready for next upgrades =====> Upg v1 ======> Upg v2 </p>
  
  <p>Thanks for your help</p>
  
  <p><strong>Additional info:</strong>
  This is database that exists (just an example):</p>
  
  <pre><code>CREATE DATABASE Test
  GO
  
  Use Test
  GO
  CREATE SCHEMA [TestSchema] AUTHORIZATION [dbo]
  GO
  CREATE TABLE [TestSchema].[Table1](
      [Id] [uniqueidentifier] NOT NULL,
      [Column1] [nvarchar](500) NOT NULL,
      [Column2] [bit] NOT NULL,
      [Column3] [bit] NOT NULL,
   CONSTRAINT [PK_MonitorGroups] PRIMARY KEY CLUSTERED 
  (
      [Id] ASC
  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  ) ON [PRIMARY]
  GO
  </code></pre>
  
  <p>Using reverse engineering EF created initial migration :</p>
  
  <pre><code>public partial class Initial : DbMigration
      {
          public override void Up()
          {
              CreateTable(
                  "TestSchema.Table1",
                  c =&gt; new
                      {
                          Id = c.Guid(nullable: false),
                          Column1 = c.String(nullable: false, maxLength: 500),
                          Column2 = c.Boolean(nullable: false),
                          Column3 = c.Boolean(nullable: false),
                      })
                  .PrimaryKey(t =&gt; t.Id);
          }
  
          public override void Down()
          {
              DropTable("TestSchema.Table1");
          }
      }
  </code></pre>
  
  <p>if I use the code provided by @spender against non existing database everything is cool .
  If I use it against existing database it works until i change the model (next migration).</p>
  
  <p>What I have seen is that upgrade script returned by migration contains entire database creation. And can not be executed against already existing objects.</p>
  
  <p>What can actually work is to add migration table to existing database and add initial data, but I am not sure that this is a good solution.  </p>