5

Possible Duplicate:
EF 4.1 Code-first vs Model/Database-first

I just start learning about EF 4.0 CodeFirst, I want to hear from experts about What are the EF CodeFirst advantages and disadvantages ?

Community
  • 1
  • 1
Saber Amani
  • 6,409
  • 12
  • 53
  • 88

3 Answers3

8

I think each approach is more suited to different scenarios:

Scenarios in which Database-first or Model-First are recommended:

  • Long lasting databases with stable schemas
  • Changes to database and model are incremental
  • You want to be able to see your model in one diagram and update from there
  • You can divide your model to non-overlapping diagrams

Scenarios in which Code-first is recommended:

  • Development of model is done by programmers that are not interested in database
  • Model classes contain logic
  • Model classes have bizarre (non-standard) structures
  • Model is divided between many assemblies that are not all known at design time (extensions)
  • Databases are short lived (e.g. run-time of application)
  • Database can change often

If you want a long lasting database which is dynamic enough to contain ever-changing structure, then consider generic classes/tables that your model/schema reuses for varying/context-dependant purposes.


Update:

I now recommend using Code-First for additional cases:

  • When you want to be able to write and quickly and easily run integration tests on LocalDB (instead of working with SSDT)
  • If you rather see the model and the mappings in a single place instead of going over diagrams and mapping windows
    • If you want it to be more visible if someone mapped properties correctly or not e.g. as ConcurrencyCheck
  • Since you can easily disable schema generating in non-local environments, the character of the database is less relevant
Danny Varod
  • 17,324
  • 5
  • 69
  • 111
5

I've always been an advocate for Data Centric approach, and I believe this is where the advantages and disadvantages come from.

If your strengths are in Database Design and Development, you may find it easier and more intuitive to develop from a database schema. However, if you think better in objects and classes, or if you are working directly from a class model you may be better off starting from a CodeFirst perspective.

Personally, I find myself having to make more changes working from a CodeFirst standpoint that from a "DataFirst" generating my classes.

TreK
  • 1,144
  • 2
  • 13
  • 25
0

The main difference is that Code First requires that you define everything in code, whereas Model/Database first require you to define everything in XML/Designer, and only define minimal things in code.

For example, if you want two way navigation properties, then you have to write the code for that in each of your entities. This is generated automatically for you when using the other methods.

I like the power Code first gives you, but most of the time i just can't be bothered to do all that.

For the most part, you get exactly the same thing. It's just how you model it.

The other disadvantage is that (currently) in Code first, when you make changes, it drops the table and recreates it. Losing all data (you can of course seed the database, but you won't get back data you might have entered manually). This will be solved when the Code First Migrations product is released.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • I don't agree that one of the disadvantages is that it drops the database. This is something that is completely configurable and by default it won't drop the database but it will throw an error (just as model first and database first will do when they detect changes). – Wouter de Kort Jan 09 '12 at 19:11
  • @WouterdeKort - My point was that in database first, a database change is easily updated in the model and new code generated, all pretty much automatically. In code first, it's a more manual process, unless you just let it drop the database and recreate it. You have to make the changes manually in both code and the database. – Erik Funkenbusch Jan 09 '12 at 19:18