Introducing the Entity Framework

The Entity Framework is a set of technologies in ADO.NET that support development of data-oriented software applications. Architects and developers of data-oriented applications have struggled with the need to achieve two very different objectives. They must model the entities, relationships, and logic of the business problems they are solving, and they must also work with the data engines used to store and retrieve the data. The data may span multiple storage systems, each with its own protocols; even applications that work with a single storage system must balance the requirements of the storage system against the requirements of writing efficient and maintainable application code.

The Entity Framework enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored. This is enabled by elevating the level of abstraction at which developers can work when they deal with data and reducing the code that is required to create and maintain data-oriented applications. Because the Entity Framework is a component of the .NET Framework, Entity Framework applications can run on any computer on which the .NET Framework 3.5 Service Pack 1 (SP1) is installed.

Giving Life to Conceptual Models

A longstanding and common design pattern for data modeling is the division of the data model into three parts: a conceptual model, a logical model, and a physical model. The conceptual model defines the entities and relationships in the system that is being modeled. The logical model for a relational database normalizes the entities and relationships into tables with foreign key constraints. The physical model addresses the capabilities of a particular data engine by specifying storage details such as partitioning and indexing.

The physical model is refined by database administrators to improve performance, but programmers writing application code primarily confine themselves to working with the logical model by writing SQL queries and calling stored procedures. Conceptual models are generally used as a tool for capturing and communicating the requirements of an application, frequently as inert diagrams that are viewed and discussed in the early stages of a project and then abandoned. Many development teams skip creating a conceptual model and begin by specifying tables, columns, and keys in a relational database.

The Entity Framework gives life to conceptual models by enabling developers to query entities and relationships in the conceptual model while relying on the Entity Framework to translate those operations to data source-specific commands. This frees applications from hard-coded dependencies on a particular data source. The conceptual model, the storage model, and the mapping between the two are expressed in an external specification, known as the Entity Data Model (EDM).The storage model and mappings can change as needed without requiring changes to the conceptual model, data classes, or application code. Because storage models are provider-specific, you can work with a consistent conceptual model across various data sources.

An EDM is defined by the following three model and mapping files that have corresponding file name extensions:

  • Conceptual schema definition language file (.csdl) - defines the conceptual model.

  • Store schema definition language file (.ssdl) - defines the storage model, which is also called the logical model.

  • Mapping specification language file (.msl) - defines the mapping between the storage and conceptual models.

The Entity Framework uses these XML-based model and mapping files to transform create, read, update, and delete operations against entities and relationships in the conceptual model to equivalent operations in the data source. The EDM even supports mapping entities in the conceptual model to stored procedures in the data source. For more information, see Data Modeling in the Entity Framework.

Mapping Objects to Data

Object-oriented programming poses a challenge for interacting with data storage systems. Although the organization of classes frequently closely mirrors the organization of relational database tables, the fit is not perfect. Multiple normalized tables frequently correspond to a single class, and relationships between classes are represented differently from relationships between tables. For example, to represent the customer for a sales order, an Order class uses a property that contains a reference to an instance of a Customer class, but an Order table row in a database contains a foreign key column (or set of columns) with a value that corresponds to a primary key value in the Customer table. A Customer class might have a property named Orders that contains a collection of instances of the Order class, but the Customer table in a database has no comparable column.

Existing solutions have tried to bridge this gap, which is frequently called an "impedance mismatch", by only mapping object-oriented classes and properties to relational tables and columns. Instead of taking this traditional approach, the Entity Framework maps relational tables, columns, and foreign key constraints in logical models to entities and relationships in conceptual models. This enables greater flexibility both in defining objects and optimizing the logical model. The Entity Data Model tools generate extensible data classes based on the conceptual model. These classes are partial classes that can be extended with additional members that the developer adds. The classes that are generated for a particular conceptual model derive from base classes that provide Object Services for materializing entities as objects and for tracking and saving changes. Developers can use these classes to work with the entities and relationships as objects related by navigation properties. For more information about Object Services, see Object Services Overview (Entity Framework).

Accessing and Changing Entity Data

More than just another object-relational mapping solution, the Entity Framework is fundamentally about enabling applications to access and change data that is represented as entities and relationships in the conceptual model. Object Services uses the EDM to translate object queries against entity types that are represented in the conceptual model into data source-specific queries. Query results are materialized into objects that Object Services manages. The Entity Framework provides the following ways to query an EDM and return objects:

  • LINQ to Entities - provides Language-Integrated Query (LINQ) support for querying entity types that are defined in a conceptual model. For more information, see LINQ to Entities Overview.

  • Entity SQL - a storage-independent dialect of SQL that works directly with entities in the conceptual model and that supports EDM features such as inheritance and relationships. Entity SQL is used both with object queries and queries that are executed by using the EntityClient provider. For more information, see Entity SQL Overview.

  • Query builder methods - enables you to construct Entity SQL queries using LINQ-style query methods. For more information, see Query Builder Methods (Entity Framework).

The Entity Framework includes the EntityClient data provider. This provider manages connections, translates entity queries into data source-specific queries, and returns a data reader that Object Services uses to materialize entity data into objects. When object materialization is not required, the EntityClient provider can also be used like a standard ADO.NET data provider by enabling applications to execute Entity SQL queries and consume the returned read-only data reader. For more information, see EntityClient Provider for the Entity Framework.

The following diagram illustrates the Entity Framework architecture for accessing data:

Entity Framework Architectural Diagram

The Entity Framework generates a class derived from ObjectContext that represents the entity container in the conceptual model. This object context provides the facilities for tracking changes and managing identities, concurrency, and relationships. This class also exposes a SaveChanges method that writes inserts, updates, and deletes to the data source. Like queries, these changes are either made by commands automatically generated by the system or by stored procedures that are specified by the developer. For more information, see Adding, Modifying, and Deleting Objects (Entity Framework).

Entity Data Model Tools

Together with the Entity Framework runtime, the .NET Framework 3.5 SP1 includes EDM Generator (EdmGen.exe). This command prompt utility connects to a data source and generates an EDM based on a one-to-one mapping between entities and tables. It also uses a conceptual model file (.csdl) to generate an object layer file that contains classes that represent entity types and the ObjectContext. For more information, see EDM Generator (EdmGen.exe).

Visual Studio 2008 includes rich tool support for generating and maintaining an EDM in a Visual Studio application. The Entity Data Model Designer supports creating advanced mapping scenarios, such as table-per-type and table-per-hierarchy inheritance and split entities that map to multiple tables. For more information, see ADO.NET Entity Data Model Designer Overview.

Learning More

The following topics enable you to learn more about the Entity Framework:

Getting Started (Entity Framework)

Provide information about how to get up and running quickly using the Quickstart (Entity Framework), which shows how to create a simple Entity Framework application.

Quickstart (Entity Framework)

Shows you how to use the Entity Data Model tools with Visual Studio 2008 to quickly create your first Entity Framework application.

Application Scenarios (Entity Framework)

Provides task-based links to topics that match specific application scenarios, such as writing queries, binding objects to data controls, or implementing business logic.

Entity Framework Features

Provides more detailed information on the features that compose the Entity Framework and links to topics that discuss those features.

Entity Framework Terminology

Defines many of the terms that are introduced by the EDM and the Entity Framework and that are used in Entity Framework documentation.

Entity Framework Resources

Provides links to conceptual topics and links to external topics and resources for building Entity Framework applications.

See Also

Community Additions