简体   繁体   中英

Entity Framework Core: sanity-check if database and model are in sync

We're currently in a setup in which multiple systems use one huge database. In our newest system we're using Entity Framework Core with a code-first approach. Unfortunately, some changes of the database, made by the other systems, are not updated in the code-first system. As we don't have a complete integration-test coverage, these changes sometimes go unnotified until someone uses the changed entity by chance.

We can't just change the setup in a swift manner; therefore, we would like to have some automated sanity checks if the database and the models are synchronized. Searching for such a solution, I found this fine piece of code: Does an equivalent to Database.CompatibleWithModel(bool) exist in EF Core

But it is explicitly stated that this code should not be used (and it doesn't work anymore anyway). I found some more ideas from Brice here: Validate that EF Core ModelSnapshot, migrations, and actual database schema are consistent with each other . But these approaches are mostly manual ones respectively not easy to use from the code directly.

Is there already a solution around, which allows to check the synchronization between the mapping and the actual database from C# directly? Checking the Entity Framework Core interface, I think we would have all needed information available, but programming this checks in a generic manner seems like a huge task?

Unfortunately, EF will only detect and throw exceptions if a part of the schema is missing from the database that is referenced in EF. As far as fields or tables added from another system, that would be ignored.

You can consider generating a schema change report that can be emailed to your team everytime there is a change:

Send Email Alert on Database Schema Change in SQL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM