Code First versus Database First?

Asked

Viewed 680 times

12

I’m used to creating projects on Asp.net-MVC with Entity Framework Database First. From a modeling, 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 say in the practical sense, what are the pros and cons of each approach?
What mute in the architecture of a project and the way to develop the system?

  • What is missing in the answer provided?

  • The answer is great, but it’s not yet clear to me how it affects the development of the system in a team.. What about the application of system modeling and dbas? and maintenance afterwards?

  • 1

    In the Code First modeling begins in Models, the bank is generated from the system, and the DBA has only paper afterward that the system code has already been written. The idea of the Code First is one day to be able to dispense with the role of DBA, since having a system implemented using best practices results in an already optimized database.

  • Got it. But doesn’t that tend to make the database too application-dependent? If another application appears that uses the same bank, it is possible to have both with Code First without there being a risk of a change in the Model from one affecting the other through the bank?

  • There are no "two applications Code First with the same bank". As I told you, if the bank already exists, the second application is necessarily Database First.

1 answer

16


Differences

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

Pros and Cons

Code First

Pros

  • Can work independently from database technology;
  • Data validations are not strict to a technology;
  • Can build and destroy databases generated in instants, through Migrations;
  • Ideal for new applications;

Cons

  • Not well connected to an existing database;
  • It depends on a good ORM, like the Entity Framework and Nhibernate, for example;
  • Requires constant optimization, because the abstraction of data for some databases does not always have a good performance;
  • Database validation is limited. Database generation by the application is never 100%;
  • Poor database security.

Database First

Pros

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

Cons

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

What changes in the architecture of a project and the way to develop the system?

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 addenda.

In the Code First, the database tracks the system’s growth. In the Database First, the whole system needs to be generated at once, and every incremental implementation needs to be manual.

On the other hand, a system Code First is green as to the security and performance of the database. Most problems are solved by good practice, but still something leaves something to be desired, like a huge or peculiar table that requires extra optimization.

  • In the Database First, where is business logic? In Controller it is considered bad practice, but if applied in Model, it can no longer be updated by EF...

  • 2

    "Controller is considered bad practice". No. It depends. When logic involves data relationship between Models uncorrelated in definition, not bad practice. Otherwise, the other validations are divided between the) Models, and b) Filters. This was not in the scope of the question, but if you want, I can detail more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.