Model First, Database First or Code First–Update to Data Points Column

My recent Data Points column in MSDN Magazine (May 2011) provided an overview of the differences between the three workflows for creating an Entity Framework model:

1) Reverse engineer and existing database into an EDMX model

2) Create an EDMX model in the designer and generate a database from that

3) Forego a physical model and use your domain classes along with Entity Framework Code First.

After listing the big differences and discussing pros & cons of the different workflows, I presented a diagram to encapsulate the decision making.

A few days before the article became public, I received an email from a developer asking about how to create a model when he had pre-existing classes and a pre-existing database. The obvious answer (in my mind) was to use Code First. Otherwise, he would have had to try to work out a model that would match his existing classes.

I immediately realized that I did not include that suggestion in the decision tree in the article, so here is that diagram, updated.

The new decisions are in red.

image

I know that even after you’ve said that you prefer a visual designer, I’m still recommending that you don’t use it in this case (point back to code first) but believe me, you’ll most likely be happier in that scenario.

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

10 thoughts on “Model First, Database First or Code First–Update to Data Points Column

  1. Thanks for the Article Julie! Maybe I missed this part but if I have an existing DB I can still use Code First? What if I don’t have and Existing Classes? I don’t want to take your diagram too literal but I am unsure so I figured I should ask.

  2. good point, Patrick. You can definitely use CF with an existing DB!!! Since that wasn’t a decision factor (db or no db) I didn’t consider it as a choice. You can use CF either way.

  3. when it comes to design business object using EF 4.0, Do I have to make sure ObjectContext.Dispose() is called to release resources(DB connection)?

  4. @Peter.. there’s no automated way. Modify your model. Modify your db. Apply any necessary mappings.EF team says they are working on a migration story.

  5. Hi Julie,

    As I mentioned a while ago, _combining_ db-first and model-first is yet another option. Not available out-of-the-box but supported by 3rd party extensions (e.g. my add-in 🙂 ).

    I made a short screencast video showing my approach to doing _both_; starting off with an existing database and then making schema/model changes both in the EF designer and in the database.

    Here it is: huagati.blogspot.com/…/entity-framewor

  6. Hi Julie,

    Nice article! Do you have any pointers to dealing with changes to a database schema after EF classes have been generated?

    I’m running into a problem where a column was deleted from the db schema, but the EF sql generator is still including that column when creating an update query.

    Thanks!

    Dan

  7. Julie,

    I am planning to learn MVC + EF. Hearing a lot about this Code-First approach and after reading couple posts on your blog and watching some videos, I am bit confused because CF recreates the DB everytime the model changes which isn’t normal. Also till today, I have been using StoredProcedures and all of a sudden no SPs with CF.

    The diagram above in your posts helps to decide on chosing the method based on coding-preference but which method to choose based on the type of application is not clear.

    Do you think you can provide some insight on if CF is a good fit for a large website? Or may be in general for what kind of application CF is good and for which it’s not. If scaling of an application could be an issue?

    Thanks for this nice blog.

    -Om

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.