MVC3 Model compatibility cannot be checked because the database does not contain model metadata

Published on Friday, August 31, 2012

An ASP.NET MVC3 project, using code first. followed Microsoft tutorial "Getting Started with EF using MVC".

When we tried to change models, we often got the following error:

Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.

image

I have a Initial class followed the tutorial like this:

public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>

And then we put the following code in Application_Start() method in Global.aspx.cs file:

Database.SetInitializer<SchoolContext>(new SchoolInitializer());

The tutorial is not wrong, but when we develop, we could not make sure our models correct at the first time, when we run our application using wrong model, there might be a wrong database created successfully using the wrong model, so later we found the error and change our Models, then we tried to run our application again, we often got the error message that we mentioned above.

so we can choose the one of the following resolve ways:

1: Simply dropped the old database manually;  (if you could not drop, please remember to disconnect your database in Visual Studio);

2: Replace the following code:

public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>

Using this one:

public class SchoolInitializer :  DropCreateDatabaseAlways<SchoolContext>

However, both of above code should be remove when we product code.