Pavel Voronin
Pavel Voronin

Reputation: 13983

How to validate Entity Framework CodeFirst mappings against existing database?

When DbContext is instantiated it validates conceptual model only.
But when first query is executed context performs validation against Db.

Is it possible to check all the mappings against database without issuing queries?
I'd like something like context.ValidateAgainstDb(connection)

Now we do such integration testing in the following way:

We use AutoFixture to generate 'trash' entities. After that we switch constraints off and add these entities via DbSet calling SaveChanges().

In principle, this process can be to completely automated.
But before reinventing the wheel I'd like to know whether ready solution of the problem exists.

Upvotes: 3

Views: 254

Answers (1)

bubi
bubi

Reputation: 6491

DbMigrator.GetPendingMigrations().Any()

EDIT
If you are looking for a check without any migration, I think there is not. Thinking about how EF migration works (__MigrationHistory contains the POCO model) I think that EF does not have a way to know if the DBMS model is ok for the POCO model.

Upvotes: 1

Related Questions