EF Code-First Approach Vs Database First Approach
Following are some of the advantages and disadvantages on each Approaches of Entity Framework
Code-First Approach
Advantages
- No need to look at database for any changes in the tables as we can do those in our domain models and these will be migrated to database.
- Will have complete control on each table level to 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 modifications done at database it won’t effected on entities in the application.
- Should have 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 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 updates with single click at application end.
- Can use existing database.
Disadvantages
- Based on database tables, 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.