Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 956
Josh Avatar asked Jan 17 '12 16:01

Josh


People also ask

Can we use code first approach for existing database?

To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item.. Select ADO.NET Entity Data Model in the Add New Item dialog box and specify the model name (this will be a context class name) and click on Add. This will open the Entity Data Model wizard as shown below.

What is difference between EF designer from database and code first from database?

The only difference is that: using the designer you can do it graphically, and easyly set properties, column names, and so on. using Code First, you have to set these properties, columns names, data types and so on using conventions, Fluent API or attributes.

How do I update Entity Framework model from database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.

What is the difference between code first and database first?

In the code first approach, the programmer has to write the classes with the required properties first, while in the database first approach, the programmer has to create first the database using GUI.


2 Answers

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.

like image 125
bricelam Avatar answered Oct 13 '22 11:10

bricelam


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.

like image 40
Ibrahim Najjar Avatar answered Oct 13 '22 11:10

Ibrahim Najjar