Code First vs Database First vs Model First Approach – Using Entity Framework Core

Code First and Database First are 2 terms that you will come across quite often while working with object-relational mapping frameworks such as Entity Framework. You will often find the developers and designers divided in 2 categories, most of the coders usually favor Code First while DB designers mainly support Database First.

Entity Framework can be implemented via 3 approaches namely Code First, Database First and Model First. The development of the Model First approach in entity framework is dropped and hence Code First and Database First are the approaches that are more commonly used.

Code First Approach

In Code First approach, one can create classes and entities without having to design the database that matches these entities. The database along with all the tables automatically gets created based on the defined classes and entities as soon as the code is run. So basically, database is generated based on the code.

Example – using Entity Framework

For example, if we are working on an application that stores the passengers travelling in a tour bus, we begin by first creating a class called Passengers and define its properties as shown in the code below instead of creating a database first.

namespace CodeFirstExample
    public class Passenger
        public int PID { get; set; }
        public string PName { get; set; }
        public DateTime? DOB { get; set; }
        public DateTime? TravelDate { get; set; }
    public class TourbusContext : DbContext
        public TourbusContext()
        public DbSet<Passenger> Passengers { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            object p = optionsBuilder.UseSqlServer(@"Server=localhost;Database=TourDB;Trusted_Connection=True;");

To create the database using the above code, run below commands on the Nuget Package manager Console:

Install-Package EntityFramework

Install-Package Microsoft.EntityFrameworkCore.SqlServer

Install-Package Microsoft.EntityFrameworkCore.Tools


add-migration CreateTourDB

update-database –verbose

this creates the database from our code as below

The above working code can be found here.

Advantages of using Code First Approach

  1. There is almost no chance of conflict between the model and database. For example, conflicts between column/stored procedure names, column/parameter count while mapping the data from data response to the model.
  2. Complete and proper access over the entire code. Hence, changes can be performed easily and without any hassle.
  3. The better option for programmers who do not enjoy designing or working with the database.
  4. In code first approach, database version control can be smoothly performed which is a very crucial yet tedious operation usually. Since the database schema is entirely dependent on the code models, the database can be versioned by version controlling the source code.
  5. Very useful for smaller applications which do not deal extensively with data.
  6. Change of database is relatively easy from one database to other, like SQL to Oracle
  7. Need not care about running various scripts while deploying the application to another environment, like dev environment to staging environment

Disadvantages of using Code First Approach

  1. You have to write the code from scratch and is comparatively difficult to modify as code first approach does not produce auto generated code.
  2. Not suggested for data heavy applications and complex logic as it becomes difficult to maintain the data.
  3. Manual changes to the database are usually not recorded and are lost because the database is defined by the code.
  4. Every modification in the database can be saved by running the business entity class of the code.

Database First Approach

In Database First approach, one first creates the database and all the tables associated with it. An entity data models are then created using this database that already exists.

So in this case, database is created first and is followed by the code (that is generated automatically by the entity framework).

Now for the same passengers travelling in a tour bus example, if we follow the database first approach, the code will look somewhat similar to what is shown below. In this case, we create an empty project and then add Entity Framework package to create the models from tables.

Database First Example:

If the database is already created, we can Scaffold the Database, and entity framework core will create all the required models, and DBContext as below for our TourDB.

We run below command on the package manager console:

Scaffold-DbContext “Server=localhost;Database=TourDB;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force

Auto Generated Code in file TourDBContext.cs :

We can now use the autogenerated code as below to perform operations on the DB:

        public static void Main(string[] args)
            using (TourbusContext dbCtx = new TourbusContext())
                var passenger = new Passenger()
                    PName = "Mark",
                    DOB = new DateTime(),
                    TravelDate = new DateTime()

The above working code can be found here.

Advantages of using Database First Approach

  1. It is preferred when we migrate the application from one-server side technology to other. Since the DB is already there, we can update our code and models as per the database. Example-  a desktop application being moved to could services.
  2. It is preferred in cases where applications have extensive amount of data and complicated logic as the code gets generated automatically. It is option usually favored by designers.
  3. It is easier to create database and the associated tables than the code first approach because of all the Graphical User Interface (GUI) tools available.
  4. The databases that are already existing with similar attributes can often be reused instead of creating a fresh new database.
  5. Defining keys and relationships are much easier and use much less code (usually no extra code).

Disadvantages of using Database First Approach

  1. On adding new features, need to update database and extend the generated model class.
  2. If existing database is used to generate the model, in many cases this results in a huge amount of auto generated code due to the other associated code models.

Code First or Database First – Which one to Use?

In my opinion, a lot of professionals are focusing on code-first approach due to its overall advantages over database first approach. While there is no right or wrong answer to the question “Which approach would be more suitable for me to use?” but there is definitely a more suitable option for you based on which factors you consider more important. For example, if your project deals with large amount of complex data, you could choose database first approach, otherwise you can choose code-first approach. It is important to choose the approach solely based on what will suit your project the best.

Model First Approach

The reason that we are discussing Model first in the end because it is discontinued in Entity Framework Core, and it is rarely used while developing new applications. Model first does exist in Entity Framework and will always be there, but there are certain challenges in model first approach when it is used with Entity Framework. First let’s discuss what is model first approach.

Model first approach updates the database from model which is created using an edmx file, instead of the code. There are 2 ways to make changes on the model, either by updating the edmx file or by using an inbuilt designer available in visual studio. It looks like below:

Notice the file extension is .edmx

Now we can update the database from the model changes using “Generating database from model” option as below

This will in turn update the database as per the model changes.

Below are the disadvantages of the same:

  1. It is difficult to manage in a large team, since the code generated from this model results in a large number of merge conflicts.
  2. Since there is a single Model schema file (.edmx), it increases in size with the database and is very difficult to load/make changes.


We discussed about the 3 major approaches related to relational data access in software applications. Above code is implemented using Microsoft Entity Framework Core. Both approaches are equally important depending upon the application scenarios and use cases. If you have any suggestions please do mention in the comment box.

Keep doing the magic from code!!

Leave a Comment

Your email address will not be published.