title: Setting up a Entity Framework Code First Database on SQL Server 2008
body: <p>I successfully created my first Entity Framework Code first project using SQL Server CE. The database tables were created automatically and all was well.</p>
  
  <p>I then changed my connection string to point to a SQL Server database I'd created and expected it to autogenerate the tables. It didn't and instead complained about the tables not existing.</p>
  
  <p>After some reading I found that I needed to add an initializer to my <code>App_Start</code> method.</p>
  
  <p>I tried:</p>
  
  <pre><code>// Method 1
  Database.SetInitializer(new DropCreateDatabaseIfModelChanges&lt;EFContext&gt;());
  </code></pre>
  
  <p>But this didn't work at first because it couldn't find an EdmMetaData table to discover if the model had changed.</p>
  
  <p>So I tried adding:</p>
  
  <pre><code>// Method 2
  Database.SetInitializer(new CreateDatabaseIfNotExists&lt;EFContext&gt;());
  </code></pre>
  
  <p>But this didn't work because I'd already created the database, and when I removed the database it still didn't work because the user I was logging in as didn't have create database privileges.</p>
  
  <p>I finally tried:</p>
  
  <pre><code>// Method 3
  Database.SetInitializer(new DropCreateDatabaseAlways&lt;EFContext&gt;());
  </code></pre>
  
  <p>Which worked, but I don't really want to drop the db every time. So once it ran and added the EdmMetaData table I replaced it with method 1 and that now works fine.</p>
  
  <p>Although this method works, it doesn't feel as elegant as when I was using SQL Server CE and I've not found someone suggesting that you use this method.</p>
  
  <p>So, am I missing something and is there an easier, more elegant way to do this?</p>