EF Code-First Approach Vs Database-First Approach

Let us discuss some of the advantages and disadvantages of Code-First approach and Database-First approach of Entity Framework.

Code-First Approach

Advantages

  1. No need to look at the database for any changes in the tables as we can do those in our domain models and these will be migrated to the database.
  2. Provides complete control on each table level of the set, for example, lazy loading, serialization etc.
  3. All changes will be tracked in the database such that we can roll back to any version as needed.
  4. We don’t need any heavy .edmx files and T4 script executions.
  5. Can do changes in the database with no data loss (this feature is available since EF 4.3).

Disadvantages

  1. If there are some 100’s of tables, we need to manually create the domain models.
  2. Writing database objects like Stored Procedures, triggers are complex.
  3. If there is any modification done in the database, it won’t reflect on the entities in the application.
  4. Should have the good C# knowledge to write code while Migrations.
  5. Not preferred for data-intensive applications.

Database First Approach

Advantages

  1. Easy to create Domain Models as this will be automatically created while creating an edmx file using T4 scripting.
  2. Visual Studio provides GUI to configure and add database via. Edmx file.
  3. Good for larger applications.
  4. Any change at database side can be easily updated with a single click at application end.
  5. Can use an existing database.

Disadvantages

  1. Based on database tables, the edmx file will keep growing.
  2. Creating/managing associations, foreign keys, constraints will be more difficult.
  3. If the database is large, it is not easy to maintain or update the edmx file.