0

I have a model.edmx file generated using ef6 database first approach. The database is used by a number of apps and I have no control over when or how the schema may change.

There is a GUI wizard to "update model from database" which works for my purpose. The wizard detects differences between my model and the database, so if I add a table directly to the database, or add/remove a column from some existing table I can see those changes in the "update model from database" wizard.

Is there a way to detect these changes programmatically? and is it possible to update the schema programmatically? or just regenerate it altogether to match the database?

At the least, is it possible to programmatically distinguish that the model is out of sync with the db, using existing EF libraries, rather than writing my own custom schema compare.

Thank you.

sheldonzy
  • 5,505
  • 9
  • 48
  • 86
  • I think `bool isCompatible = context.Database.CompatibleWithModel(true);` will work for a db-first approach to EF, though it throws exceptions. [Source](https://stackoverflow.com/a/25445651/3191303) – AWinkle May 24 '17 at 18:51
  • Are you looking to programmatically change your db schema or your .NET objects when a schema difference is detected? Updating your .NET classes, at run time, isn't really possible - a new version of the code base should have been compiled against the new db schema. If you are looking to update your db schema programatically, that is more what code-first does. – AWinkle May 24 '17 at 18:54
  • {"Model compatibility cannot be checked because the DbContext instance was not created using Code First patterns. DbContext instances created from an ObjectContext or using an EDMX file cannot be checked for compatibility."} – Krzysiek Derkowski May 24 '17 at 18:54
  • thanks, I would settle for just being informed at run time that my code is running against an outdated database schema, I can make a more informed decision rather than have the application explode when it's missing a table or a column because it has been renamed. I would like to update the schema programatically so it will error on building if any referenced columns or tables have been removed from the database. I am stuck with db-first, I can't make schema changes, my app just reads. – Krzysiek Derkowski May 24 '17 at 19:02
  • Been there. I found a [code review.SO](https://codereview.stackexchange.com/questions/45831/verifying-the-entity-data-model-schema-against-a-production-database) post that might be helpful. – AWinkle May 24 '17 at 19:09

0 Answers0