Josh
Josh

Reputation: 2339

Validate EF Code first model against existing database

Is there a way to check that a DbContext matches the database when the database was not created by EF code first?

I am looking for similar functionality to Database.CompatibleWithModel but there is not metadata.

Upvotes: 12

Views: 586

Answers (2)

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

Have you tried using Entity Framework Power Tools.

you can use the tools to Reverse Engineer Code First - Generates POCO classes, derived DbContext and Code First mapping for an existing database.

And then maybe you can compare the reversed engineered information with what you already have.

Upvotes: 1

bricelam
bricelam

Reputation: 30375

There is currently no way in EF to do this; however, you may be able to use the DDL script as a starting point for verifying that all the artifacts exist in the database. To get this script, use

string ddlScript = ((IObjectContextAdapter)myContext).ObjectContext.CreateDatabaseScript();

Some tools may be able to use this script to do a schema compare against your database. This will tell you if your model is compatible.

Upvotes: 7

Related Questions