How to check if database schema matches Entity Framework schema?

There are two tools to do this. This first one is popular and highly developed:

the developer's explanation: https://www.thereformedprogrammer.net/ef-core-taking-full-control-of-the-database-schema/

As you'll see, the developer's own explanation includes a thorough overview of Code-First, Database-First, and SQL-First approaches. He discusses pros and cons of all approaches. And shows why the Schema Compare tool is necessary to use a SQL-First approach.

the github project: https://github.com/JonPSmith/EfCore.TestSupport/wiki/9.-EfSchemaCompare

And there is a lesser-known second one mentioned by another commentor above: https://github.com/reckface/EntityFramework.Verify

The second developer also suggests DbUp, which offers a Philosophy article that I think is a worthwhile read on why the "Microsofty" code-first and db-first approaches are problematic, and why the idea of viewing database changes as a state system is arguably a poor choice. https://dbup.readthedocs.io/en/latest/philosophy-behind-dbup/


EF does not cross check database schema with model each time you start your application. Instead it is looking for the model that is saved to the database (__MigrationsHistory table and before EdmMetadata) and compare this saved model with the model you are using. If models match then database will be used. If models don't match an exception will be thrown. If you have neither __MigrationHistory nor EdmMetadata table in your database EF will assume that you are using Database first approach with DbContext and your database matches the model. If you want to compare the database with your model you could dump Edmx for your model (using EdmxWriter.WriteEdmx) and use the Visual Studio and EF designer get the Edmx from Database and compare SSDL parts.


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);