Data Haunting

Easy Solutions Destination


Difference between code first and database first and model first approach in entity framework

Entity Framework has mainly three different types of design approaches like Code First Approach, Database First Approach and Model First Approach. In this article we will compare all these approaches with advantages and disadvantages and will try to understand the basic difference of Code First Approach vs Database First Approach vs Model First Approach in Entity Framework.

Code First Approach

Code First is very popular approach of Entity Framework because it has full control over database. In code first approach first entity classes are created with properties which is called POCO Entity From this POCO classes Entity framework will create the database and tables. So database is generated from the code. Database is created from Code first migration command using Package Manager console.

Advantages of Code First Approach

  1. Full control over the code (no auto generated code which is hard to modify).
  2. Using Code First we can create the database and tables from your business objects.
  3. Code first have control over which related collections are to be eager loaded, or not be serialized at all.
  4. .Does not need to worry with General expectation with DB. DB is just a storage with no logic. EF Code First will handle creation.
  5. Easily can maintain Database different different version control.

Disadvantages of Code First Approach

  1. First you need to learn Fluent API of Entity Framework properly than you can implement Code First.
  2. Everything related to database you have to code in the visual studio.
  3. It is very hard to maintain stored procedures in code first approach because you have to map stored procedure using Fluent API and write Stored Procedure’s script inside the Visual studio’s code.
  4. If you have made any changes in Database without made changes in Visual studio code than It will over write you database on your next migrations using code first.
  5. Any time you want to change anything in the database tables you have to make changes in the entity framework classes’s code file and run the update-database from the package manager console.
  6. Not advisable for Data intensive applications.

Database First Approach

If you have already well designed database structure than Database First approach is the best. Using this approach first you have to create Datbase and Tables from that you can create entity Data Model.

Advantages of Database First Approach

  1. Very easy approach compare to others if you have sufficient knowledge of Database.
  2. Does not need to learn Entity Framework’s Fluent API with Database First approach.
  3. Very Simple to create the POCO classes using Database First approach
  4. Does not need to give extra effort for creation and mapping of classes and thier keys and relationship. Simple to create the data model.
  5. Very nice Graphical user interface.
  6. Advisable for data intense and large applications.

Disadvantages of Database First Approach

  1. Every time when database is changed you need to regenerate to .edmx model which takes so much time, once you regenerate the edmx than only you will see changes in your classes.
  2. when you generate a .edmx model file than associated code models results in a giant pile of auto generated code.

Model First Approach

Using Model First approach, First you need to draw your model and than workflow let to decide to generate your database. In Model First Approach You cannot make manual change into database because of you don’t have command on database, but you can made the changes into model classes. Using the entity framework designer, you can create your model classes and based on your model classes your database will be generated.

Advantages of Model first Approach

  1. If you are designer fan and don’t like to write code or SQL Script than this is the best approach.
  2. You will “draw” your model and let workflow to generate your database script and T4 template to generate yout POCO entities.
  3. Very Productive for small easy projects.

Disadvantages of Model first Approach

  1. You will lose control on both your entities and database.
  2. If you want additional features in POCO entities you must use either T4 modify template or use partial classes.

What is your opinion ?

Here we have seen the diiference between Code First, Database First and Model First approaches with advantages and disadvantages of Entity Framework. I hope this will be useful to you understand the basic concepts Code First vs Database First vs Model First approach. If you have any query and question, you can comment or you can mail me.

Leave a Reply