Let us discuss some of the advantages and disadvantages of Code-First approach and Database-First approach of Entity Framework.
Code-First Approach
Advantages
- No need to look at the database for any changes in the tables as we can do those in our domain models and these will be migrated to the database.
- Provides complete control on each table level of the set, for example, lazy loading, serialization etc.
- All changes will be tracked in the database such that we can roll back to any version as needed.
- We don’t need any heavy .edmx files and T4 script executions.
- Can do changes in the database with no data loss (this feature is available since EF 4.3).
Disadvantages
- If there are some 100’s of tables, we need to manually create the domain models.
- Writing database objects like Stored Procedures, triggers are complex.
- If there is any modification done in the database, it won’t reflect on the entities in the application.
- Should have the good C# knowledge to write code while Migrations.
- Not preferred for data-intensive applications.
Database First Approach
Advantages
- Easy to create Domain Models as this will be automatically created while creating an edmx file using T4 scripting.
- Visual Studio provides GUI to configure and add database via. Edmx file.
- Good for larger applications.
- Any change at database side can be easily updated with a single click at application end.
- Can use an existing database.
Disadvantages
- Based on database tables, the edmx file will keep growing.
- Creating/managing associations, foreign keys, constraints will be more difficult.
- If the database is large, it is not easy to maintain or update the edmx file.