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.
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.
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.
Rajendran SPosted Sep 22, 2021, 8:03 AM
This is suitable for Multi-tenant database approach application
ParthaPosted Sep 7, 2021, 11:41 AM
What about model first approach
Ravindar KumarPosted Jul 24, 2018, 1:37 AM
Nice article with clear concepts.
omer kologluPosted May 5, 2017, 4:28 PM
Hard to add attribute to generated class because, when you update schema it will be overrited. but it's easy to add property. generated model classes are "partial class". just create a folder and create your model class inside. use the same namespace with model class.