Entity Framework is the Object Relational Mapping for the application built in .NET and .NET Core technologies which provides the ease and rapid development in interaction with the database.
Following are the key points stating the Pros and Cons with the analyogy and differences :
Code First
- Popular in developers as they avoid designers to define mapping in EDMX XML.
- Full control over the code (no autogenerated code which is hard to modify).
- Developer does not need to worry about database constraints and relations as Entity Framework will handle creation without the need to know how it does the job.
- Manual changes to database will be lost because it was built through the code and would be rebuilt from code during depoyment.
Database first
- Popular if you have database designed by DB Administrators, or in case of an existing Database.
- Entity Framework creates POCO Entity Objects.
- POCO Entities can modified by either T4 modify template or use partial classes.
- Manual changes to the database is acceptable as the database defines your domain model. You can always update model from database (this feature works quite well).
Model First
- Draw your database model and let workflow generate your database script and T4 template generate your POCO Entities. You will lose part of the control on both your entities and database but for small easy projects will be very productive.
- Ddditional properties in POCO entities must be done by either T4 modify template or partial classes.
- Manual changes to database will be lost because dtabase is defined by model. This requires Database generation power pack installation. It allows updating database schema (instead of recreating) or updating database projects in Visual Studio.
In case of Entity Framework 4.1 there are several other features related to Code First vs Model/Database First. Fluent API used in Code first does not offer all features of EDMX. It is expected that features like stored procedures mapping, query views, defining views etc. works when using Model/Database first and DbContext, but does not in Code first.
Database Versioning
One major benefit of Code-First approach is Versioning of the database, code-first migrations. Because database schema is fully based on code models and version controlling source code is versioning the database.
