asp.net-mvc – Code First versus Database First?

Question:

I am used to creating projects in ASP.NET-MVC with Entity Framework Database First. Based on a model, the database and the system are created.

However, I see a lot of programmers prefer Code First, where the database depends on the system.

What are the differences between the two?
I mean in a practical sense, what are the pros and cons of each approach?
What changes in the architecture of a project and in the way the system is developed?

Answer:

differences

  • Code First: Code is written first. The database is generated from code;
  • Database First: The database is written first. Application code is generated from the database.

Pros and cons

Code First

pros

  • It can work independently of database technology;
  • Data validations are not strict to one technology;
  • It can build and destroy generated databases in an instant through Migrations;
  • Ideal for new applications;

cons

  • It doesn't dock well with an existing database;
  • It depends on a good ORM, like Entity Framework and NHibernate, for example;
  • It requires constant optimization as data abstraction for some databases does not always perform well;
  • Database validation is limited. The application's database generation is never 100%;
  • Weak database security.

Database First

pros

  • Ideal for existing performance and schemas;
  • Application can be made on top of a database that already exists;
  • Better adjusted bank-level security;

cons

  • Environment generation is limited; Complex for determining incremental changes to both the database and the application;
  • Totally dependent on bank technology;
  • Limitation of bank providers that can generate the application (ODBC helps, but doesn't completely solve the problem);
  • Data validation generated for the limited application;

What changes in the architecture of a project and in the way the system is developed?

I believe that the previous items answer this question well. It all depends on the reality in which the new system is built, with a few more additions.

In Code First , the database keeps pace with the system's growth. In Database First , the entire system needs to be generated at once, and every incremental implementation needs to be manual.

In contrast, a Code First system is green in terms of database security and performance. Most problems are solved by good practice, but still there is always something lacking, like a huge or quirky table, that requires extra optimization.

Scroll to Top