Search code examples
entity-framework-4.1ef-code-firstef-database-first

what is advantage of CodeFirst over Database First?


I was watching some videos and tutorials for EF 4.1, and I do not understand any benefit of CodeFirst (except some if DB is very small 3-4 tables and I am lazy for creating DB first).

Mostly, the best approach so far is to create Database in some sort of Database editor, which is sure faster then editing in the Entity Model and EF picks up every relationships and creates associations correctly. I know there are challenges in naming convention etc, but I feel its very confusing to manage Code First as everything looks like code and its too much to code either.

What is it that CodeFirst can do and Db first cannot?


Solution

  • CodeFirst cannot do anything that DB first cannot. At the end of the day they are both using the Entity Framework.

    The main advantages of using codefirst are:

    • Development Speed - You do not have to worry about creating a DB you just start coding. Good for developers coming from a programming background without much DBA experience. It also has automatic database updates so whenever you model changes the DB is also automatically updated.
    • POCOs - The code is a lot cleaner you do not end up with loads of auto-generated code. You have full control of each of your classes.
    • Simple - you do not have a edmx model to update or maintain

    For more info see Code-first vs Model/Database-first and here Code-First or Database-First, how to choose?