How to check if database schema matches Entity Framework schema?

database-schema, ef-code-first, entity-framework, initializer

Solution

You can call CompatibleWithModel to determine if the database matches the model. If you set the parameter to true it will throw an exception if no model data is found in the database.

bool isCompatible = context.Database.CompatibleWithModel(true);

Problem

For my surprise, using the CreateDatabaseIfNotExists context initializer, the line ``` context.Database.Initialize(true) ``` doesn't throw an exception if the schema does not match my code first schema. Is there a way to validate if the current database matches our schema before, for instance, we try to access a entity, whose table doesn't exist on the database anymore, and an exception is thrown by EF?

Original source