What is Entity Framework
Before the Entity Framework, developers often used to write ADO.Net code or Data Access Block to save or retrieve data from database. But it was time consuming and may cause lots of problems.
The Entity Framework is a persistence framework allows you to avoid doing dirty work like:
- Stored Procedures
- Manage Database Connections
- Manual Mapping
The Entity Framework (EF) enables developers to work with data using objects of domain specific classes without focusing on the database tables. With EF, developers can work at a high level od abstraction and can create and maintain data-oriented applications with less code.
How EF works
- Map domain classes to the db schema
- Translate and execute LINQ queries to SQL
- Track changes and save changes
Entity Data Model (EDM)
EDM is an in-memory representation of the entire metadata:
- Conceptual Model: The conceptual model contains the model classes and their relationships
- Storage Model: The storage model is the database design model which includes tables, views, stored procedures
- Mapping: Mapping consists of information about how the conceptual model is mapped to the storage model
- Database First (traditional approach)
- Desgin tables
- EF generates domain classes
- Code First (Very new and useful)
- Create domain classes
- EF generates database tables
- Model First (Not recommend)
- Create a UML diagram
- EF generates domain classes and database
Database First or Code First
Code First Advantages:
- Full versioning of database: You can bring your database to any version with running only one comment. It's extremely helpful if you are maintaining different versions of an application.
- Productivity: Much faster to write code than use the mouse and a table designer.
There's not a best way, it depends on the situations.