Entity Framework Code-first vs Model/Database-first (continued)

In the last post we discussed the four modes of entity framework:

  • Database First
  • Model First
  • Code First from Empty
  • Code First from Database

In this post I want to list the main features of the modes above for comparison. The following contents refer to StackOverFlow.


Database First

  • Very popular if you have DB designed by DBAs, developed separately or if you have existing DB.
  • You will let EF create entities for you and after modification of mapping you will generate POCO entities.
  • If you want additional features in POCO entities you must either modify T4 template or use partial classes.
  • Manual changes to the database are possible because the database defines your domain model. You can always update model from database (this feature works quite good).
  • I often use this together VS Database projects (only Premium and Ultimate version).

Model First

  • IMHO popular if you are designer fan (= you don’t like writing code or SQL).
  • You will “draw” your model and let workflow generate your database script and T4 template generate your POCO entities. You will lose part of the control on both your entities and database but for small easy projects you will be very productive.
  • If you want additional features in POCO entities you must either modify T4 template or use partial classes.
  • Manual changes to database will be most probably lost because your model defines the database. This works better if you have Database generation power pack installed. It will allow you updating database schema (instead of recreating) or updating database projects in VS.

Code First from Empty

  • Very popular because hardcore programmers don’t like any kind of designers and defining mapping in EDMX, xml is too complex.
  • Full control over the code (no auto-generated code which is hard to modify).
  • General expectation is that you do not bother with DB. DB is just a storage with no logic. EF will handle creation and you don’t want to know how it does the job.
  • Manual changes to database will be most probably lost because your code defines the database.

Code First from Database

  • Very popular if you have DB designed by DBAs, developed separately or if you have existing DB.
  • Hardcore programmers don’t like any kind of designers and defining mapping in EDMX, xml is too complex.
  • The POCO entities will generated automatically for you, which is productive.
  • Full control over the code.
  • General expectation is that you do not bother with DB. DB is just a storage with no logic. EF will handle creation and you don’t want to know how it does the job.
  • Manual changes to database will be most probably lost because your code defines the database.

The simple “decision tree” by Julie Lerman, the author of Programming Entity Framework should help making the decision with more confidence.

Leave a Reply

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