Code-first vs Model/Database-first [closed] Ask Question

Code-first vs Model/Database-first [closed] Ask Question

What are the pros & cons of using Entity Framework 4.1 Code-first over Model/Database-first with EDMX diagram?

I'm trying to fully understand all the approaches to building data access layer using EF 4.1. I'm using Repository pattern and IoC.

I know I can use code-first approach: define my entities and context by hand and use ModelBuilder to fine-tune the schema.

I can also create an EDMX diagram and choose a code generation step that uses T4 templates to generate the same POCO classes.

In both cases I end up with POCO object which are ORM agnostic and context that derives from DbContext.

Database-first seems to be most appealing since I can design database in Enterprise Manager, quickly synch the model and fine-tune it using the designer.

So what is the difference between those two approaches? Is it just about the preference VS2010 vs Enterprise Manager?

ベストアンサー1

I think the differences are:

Code first

  • 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 autogenerated 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.

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 T4 modify 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 well).
  • 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 T4 modify template or use partial classes.
  • モデルがデータベースを定義しているため、データベースへの手動変更は失われる可能性が高くなります。データベース生成パワーパックがインストールされている場合は、これがより適切に機能します。これにより、データベース スキーマを更新 (再作成する代わりに) したり、VS でデータベース プロジェクトを更新したりできるようになります。

EF 4.1 の場合、Code First と Model/Database First に関連する機能が他にもいくつかあると予想しています。Code First で使用される Fluent API は、EDMX のすべての機能を提供しているわけではありません。Model/Database First を使用する場合、ストアド プロシージャ マッピング、クエリ ビュー、ビューの定義などの機能が動作すると予想していますDbContext(まだ試していません) が、Code First では動作しません。

おすすめ記事