There are 3 approaches through which Entity framework is implemented.
- Database First
- Code First
- Model First
Of these Database first and Code First are the most used ones. In this article I will not discuss model first approach.
First let us understand what are code first and database first
Code First Approach
In code first approach we will first create entity classes with properties defined in it. Entity framework will create the database and tables based on the entity classes defined. So database is generated from the code. When the dot net code is run database will get created.
Advantages
- You can create the database and tables from your business objects.
- You can specify which related collections are to be eager loaded, or not be serialized at all.
- Database version control.
- Good for small applications.
Disadvantages
- You have to write everything related to database in the visual studio code.
- For stored procedures you have to map stored procedure using Fluent API and write Stored Procedure inside the code.
- If you want to change anything in the database tables you to make changes in the entity classes in the code file and run the update-database from the package manager console.
- Not preferred for Data intensive applications.
Database First Approach
In this approach Database and tables are created first. Then you create entity Data Model using the created database.
Advantages
- Simple to create the data model
- Graphical user interface.
- Mapping and creation of keys and relationships are easy as you need not have to write any code .
- Preferred for data intense and large applications
Disadvantages
- Using an existing database to generate a .edmx model file and the associated code models results in a giant pile of auto generated code.
- When you need to add any functionality to generated model you have to extend the model class generated.
Choosing the appropriate approach is purely based on the applications you are developing.