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
- Full control over the code (no auto generated code which is hard to modify).
- Using Code First we can create the database and tables from your business objects.
- Code first have control over which related collections are to be eager loaded, or not be serialized at all.
- .Does not need to worry with General expectation with DB. DB is just a storage with no logic. EF Code First will handle creation.
- Easily can maintain Database different different version control.
Disadvantages of Code First Approach
- First you need to learn Fluent API of Entity Framework properly than you can implement Code First.
- Everything related to database you have to code in the visual studio.
- 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.
- 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.
- 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.
- 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
- Very easy approach compare to others if you have sufficient knowledge of Database.
- Does not need to learn Entity Framework’s Fluent API with Database First approach.
- Very Simple to create the POCO classes using Database First approach
- Does not need to give extra effort for creation and mapping of classes and thier keys and relationship. Simple to create the data model.
- Very nice Graphical user interface.
- Advisable for data intense and large applications.
Disadvantages of Database First Approach
- 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.
- 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
- If you are designer fan and don’t like to write code or SQL Script than this is the best approach.
- You will “draw” your model and let workflow to generate your database script and T4 template to generate yout POCO entities.
- Very Productive for small easy projects.
Disadvantages of Model first Approach
- You will lose control on both your entities and database.
- 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.