title: MVC 4 code first database initializer doesn't work
body: <p>I had to stop at the same stages following different MVC 4 code first technique tutorials, because database initialization failed.</p>
  
  <p>Using the connection</p>
  
  <pre><code>&lt;add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-DbTestApp-20130205173443;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-DbTestApp-20130205173443.mdf" providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>I can't even create or manage the database, which I want to be generated from my models</p>
  
  <pre><code>public class Category
      {
          public int Id { get; set; }
  
          [Required]
          [MinLength(4)]
          [MaxLength(64)]
          public string Name { get; set; }
  
          public virtual IEnumerable&lt;Article&gt; Articles { get; set; }
      }
  
      public class Article
      {
          public int Id { get; set; }
  
          public int CategoryId { get; set; }
          public virtual Category Category { get; set; }
  
          [Required]
          [MinLength(4)]
          [MaxLength(64)]
          public string Title { get; set; }
  
          [Required]
          [MinLength(16)]
          [MaxLength(1024)]
          [DataType(DataType.MultilineText)]
          public string Content { get; set; }
  
          [Required]
          [Display(Name = "Post Anonymous?")]
          public bool IsAnonymous { get; set; }
  
          public int AuthorId { get; set; }
          public virtual Author Author { get; set; }
  
          public virtual IEnumerable&lt;Comment&gt; Comments { get; set; }
  
      }
  
      public class Author
      {
          public int Id { get; set; }
  
          [MinLength(3)]
          [MaxLength(64)]
          public string AuthorName { get; set; }
  
          public virtual IEnumerable&lt;Article&gt; Articles { get; set; }
  
          public virtual IEnumerable&lt;Category&gt; Categories { get; set; }
      }
  
      public class Comment
      {
          public int Id { get; set; }
  
          public int ArticleId { get; set; }
          public virtual Article Article { get; set; }
  
          [Required]
          [MinLength(3)]
          [MaxLength(64)]
          public string Author { get; set; }
  
          [MaxLength(64)]
          public string Title { get; set; }
  
          [Required]
          [MinLength(4)]
          [MaxLength(512)]
          public string Content { get; set; }
      }
  </code></pre>
  
  <p>using the context below</p>
  
  <pre><code>    public class BlogContext : DbContext
      {
          public DbSet&lt;Category&gt; Categories { get; set; }
          public DbSet&lt;Article&gt; Articles { get; set; }
          public DbSet&lt;Author&gt; Authors { get; set; }
          public DbSet&lt;Comment&gt; Comments { get; set; }
  
          public BlogContext()
              : base("DefaultConnection")
          {
              Configuration.ProxyCreationEnabled = false;
          }
      }
  </code></pre>
  
  <p>I also set the initializer in Global.asax Application_Start() method:</p>
  
  <pre><code>Database.SetInitializer(new DropCreateDatabaseIfModelChanges&lt;BlogContext&gt;());
  </code></pre>
  
  <p>The problem comes when I'm trying to call</p>
  
  <pre><code>var articles = db.Articles.Include(a =&gt; a.Category).Include(a =&gt; a.Author);
  </code></pre>
  
  <p>inside my BlogController's Index() method to return a view with the list of stored articles. That happens every time calling a DB related methods, the error message is:</p>
  
  <pre><code>Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.
  </code></pre>
  
  <p>In the tutorials I found nothing about problems like that and the solutions I read couldn't solve the problem.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks</p>