سؤال

I developed a program that runs in production. The program uses EF5 Database First for database access (read only).

I would like to display an error if the .edmx scheme is different than the database scheme of the database configured in the connetion string.

Query example:

db.FirstTable.Where(x => x.Age> 4) If Age is for some reasond changed to PersonAge (due to database version mismatch), I want to fail safe.

Currently, I wait until the actual query occur (can be hours after the program is up), and then I handle the exception (No such column "Age"...).

I can try and call db.TableX.first() and look for the exception in the start up of the binary, is there more elegant way to validate scheme against the database ?

هل كانت مفيدة؟

المحلول

You may well have a challenge. While calling:

MyDbContext.Database.CompatibleWithModel(throwIfNoMetadata)

in EF5 looks like it would provide you with the information, the EF V6 version of the documentation includes this note:

If the context was created using either the Model First or Database First approach then the model is assumed to be compatible with the database and this method returns true

I cannot recall any of the EF6 change logs indicating a change in this area (this does not mean there wasn't one), so it is not clear whether this is better documentation or a real behaviour change.

Some debugging with the EF source is probably necessary: however EF was only made open source after V5 was released.

EDIT: Additional. Looking at the first version of System.Data.Entity.Database its XML doc comment include the above quote.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top