So, you are tooling around in asp.net mvc 3 Entity Framework Code First, and you make a change to your underlying model. you then get the following error when Entity Framework tries to recreate the database in Sql Server or Sql Server Express
Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.
You still have some lock on the database in some way, perhaps you still have the database open in Visual Studio (it is very easy to do this in Visual Studio 2010) or perhaps you have it open in a Sql Server Management Studio window. Perhaps Windows just doesn’t like you. In any case, it’s that database is open, and Entity Framework Code First will not drop the database if anyone is still using it.
You have to release all locks and holds on the database before Entity Framework Code First will drop the database. To do that, open up Sql Server Management Studio and paste in the following code
/****** Object: Database [MyDBname] Script Date: 02/24/2012 13:54:40 ******/
ALTER DATABASE [MyDBname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE [MyDBname] SET SINGLE_USER WITH NO_WAIT
DROP DATABASE [MyDBname]
That will isolate your database (please note that “MyDBName” is where you should put your database name) and allow Entity Framework CodeFirst to drop and recreate the database and implement your changes. Doing this WILL drop the database, so be careful.
|Written By Steve French