Db-First or Code-First

by Sachin Singh


Posted on Thursday, 31 December 2020

Tags: comparison between database-First and code-First approach

I always prefer to choose Code-First over Database-First, If your work environment is flexible enough and let you choose between these two approaches then close your eyes and go for the Code-First, it is just awesome, and especially for a developer it should be the first priority, it allows you to do some cool stuff which doesn't apply to database-First approach.

There are a lot of misconceptions spread in the community regarding these two workflows like
    • Database-First approach gives you more control over database.
    • Code-First approach is only for greenfield projects.

The above two statements are pure bullshit and have no relation to reality. If you are a developer then how come the database-First approach could give you more control over the database, sometimes the database is not even in your hand. The argument that they throw is, you can't create database objects with Code-First workflow, which is not true, you can create stored procedures, views, triggers, functions anything with the code-First flow as well and even in a better and a flexible way.

The second statement that the Code-First approach is only for a greenfield project is also imaginary, You can always reverse engineer your existing database to generate code-First models and then use code-First migrations for any subsequent changes afterwords.

Advantages of using Code-First approach

1. Full versioning of database:-

with the Database-First approach, you manually take care of different version of the database, more often you end up creating change scripts manually each time you change your database and run the appropriate change script to bring the database to that version, but this is not the case with Code-First approach, here we get migration files automatically which could be run against any database to bring it to the current version. we are free to choose the migrations we want to run against the database, for example, we can skip the last 5 migrations to bring the database to the older version.

2. productivity :-

For a developer writing c# code is always faster than writing SQL queries or use a management studio designer to create databases and tables. Here we just need to concentrate on our code and run the migration, the EF takes care of generating the best T-SQL to update the database for us. Yes, learning the code-First approach may take time for the first time, here we have to learn how the association works, how to establish a relationship between tables using data-annotations or Fluent API but once you learn it, you find more fun here.

when to choose database-first approach

Now, you must be thinking, when to Choose database-First approach then?
  • Choose the Db-First approach, when you have years of experience with SQL and handling databases or you find creating databases and tables using SQL statements more interesting than writing C# code.
  • You already have a database ready that doesn't require any further change or has been finalized by a team of well-experienced DBA's, then there is no point in choosing Code-First here.