Entity Framework: Overview

Description Article
Summary

Entity Framework is an object-relational mapping (ORM) framework that allows mapping of a Microsoft SQL Server and non-Microsoft database tables to .NET classes, and query of the resulting objects using LINQ To Entities.

Entity Framework Architecture

Description
Layer Component Description Comments
EDM (Entity Data Model) Consists into three main parts- Conceptual model, Mapping and Storage model
Conceptual Model Contains the model classes and their relationships. This will be independent from the database table design
Storage Model Database design model which includes tables, views, stored procedures and their relationships and keys
Mapping Information about how the conceptual model is mapped to storage model
LINQ to Entities Query language (L2E)used to write queries against the object model. It returns entities, which are defined in the conceptual model
Entity SQL Alternative query language. However, it is a little more difficult than L2E and also the developer will need to learn it separately
Object Service Main entry point for accessing data from the database and to return it back. This component responsible for the process materialization, which is the process of converting data returned from entity client data provider to an entity object structure
Entity Client Data Provider The main responsibility of this layer is to convert L2E or Entity SQL queries into a SQL query which is understood by the underlying database. It communicates with the ADO.NET data provider which in turn sends or retrieves data from database
ADO.Net Data Provider This layer communicates with database using standard ADO.NET.

DbContext is the newer API which should be used when using most common tasks since the API is better designed and is simpler to use.  For complex tasks it is still necessary to get ObjectContext from DbContext and use the older API.

Development Scenarios 

Entity framework (EF) is useful in three scenarios. The best approach can be achieved following the decision tree ilustrated next:

Entity Framework - Chose modelling

Code First: This scenario includes (1) targeting a database that doesn’t exist , (2) an empty database that Code First will create and/or add new tables too, or (3) an existing database.

Code First allows to define the model using C# or VB.Net POCO classes. O/R mapping can optionally be performed using DataAnnotation (attributes) on the classes and properties or by using a fluent API.

No Visual Designer is used. When using the Code First approach, the DbSet<TEntity> properties on the derived DbContext class are used to build a model following specific conventions (rules).

Refer to this and this article for details.


Database First-approach
: The developer wants to use a Visual Designer and the database is designed first than the other parts of the application.

Entity Framework allows to reverse engineer a model from an existing database. The model is stored in an EDMX file (.edmx extension) and can be viewed and edited in the Entity Framework Designer. The classes that the application interact with are automatically generated from the EDMX file:

Entity Framework Usage Scenario: Database First

Refer to this article for details.

Model First-approach:

Entity Framework allows to create a new model using the Entity Framework Designer and then generate a database schema from the model. The model is stored in an EDMX file (.edmx extension) and can be viewed and edited in the Entity Framework Designer. The classes that the application interact with  are automatically generated from the EDMX file:

Entity Framework Usage Scenario: Model First

Refer to this article for details.


Assembly Architecture

Entity Framework V5 Assemblies

Entity Framework 5.0 (EF) and prior versions was distributed at two places:

  1. The .NET Framework - core components on System.Data.Entity.dll assembly; and
  2. The EntityFramework NuGet Package which includes the EntityFramework.dll assembly.
Entity Framework V6 Assemblies
Current version EF6 is available in the Entity.Framework.dll assembly, obtained as the EntityFramework NuGet Package and is independent of the .NET Framework.
Note

Entity Framework is now an open source framework by Microsoft that can be obtaned codeplex.

References

[1] Refer to this article to install EntityFramework NuGet Package.
[2] Refer to this article for description of the DbContext API.