ADO.NET Entity Framework
- By Elisa Johnson
- May 2, 2007
Relational databases aren't designed to make programming against them easier, but to ensure performance, data consistency and concurrency. Seldom do developers work directly with data in the form that is returned from a database.
Proprietary data access layers retrieve and transform data accessed in a store into business objects. But when the underlying database changes, these layers must adapt to process the changes.
ADO.NET Entity Framework uses abstraction to scrub out complexity at the logical database layer, with a set of data access APIs in the .NET Framework 3.5 and corresponding tools in Visual Studio, code-named "Orcas."
ADO.NET is a data-access, .NET-integrated technology that enables applications to connect to and manipulate data. The ADO.NET Entity Framework will build on top of the existing ADO.NET 2.0 provider model, which enables access to myriad databases. The Entity Framework will be included in Orcas beta 1.
The ADO.NET Entity Framework brings to life the Entity Relationship Model (ER Model) that many database developers have used, for 30 years, to capture and diagram conceptual models of data, prior to mapping this information into a final logical -- or relational-model. The Entity Framework implements the ER Model in the form of the Entity Data Model (EDM), maintaining the principle concepts of Entities and Relationships as first-class data types, and allowing developers to program against the same conceptual model or business logic that was used in design.
Entity Data Model
The EDM is constructed largely of EntityTypes and Associations. Consider an application that controls inventory for a wine collector. To relate back to the ER Model, each Entity [Wine, Winery] from the ER Diagram becomes an EntityType with instances of each EntityType contained in EntitySets, and each Relationship [Wine produced by Winery] from the ER Diagram becomes an Association with instances of each Association contained in an AssociationSet. Associations maintain the same connectivity [1:1, 1:many] as in the ER Diagram, and the Attributes [Sweetness, Year, Size] from the ER Diagram become Properties in the EDM that describe each EntityType. The EDM also allows for inheritance between EntityTypes and for an EntityType to define NavigationProperties, encapsulating the functionality of the database "link table," describing how to navigate from one EntityType to another by way of an Association.
Schemas & Mapping
The Entity Framework uses three XML schema files, the CSDL, SSDL and MSL, to describe the Conceptual Model, the logical Storage Schema and the Mapping, respectively. These files can be coded manually, generated automatically from an existing relational database by the EDM wizard in Visual Studio, or generated automatically by an EDM Designer that was previewed in March at VSLive!.
The CSDL describes the EDM, including the EntitySets, EntityTypes, Associations and AssociationSets that have been defined.
The SSDL describes the tables and columns, which map to the Entities and Relationships chosen for the EDM, as they exist in the associated relational database. By limiting the tables and columns described in the SSDL, it is possible to provide a client-side view of the database different from the actual View or Database being accessed.
Finally, the MSL describes how the Entity Framework maps between the Conceptual Model of the EDM (CSDL) and the logical Storage Schema (SSDL) of the database.
The layering of the three XML schema files allows a separation between code and data, ensuring that the database schema may be changed, and the application will continue to run, with only a simple update of the mapping files.
Using the Entity Framework
Previously with ADO.NET, data from the relational database was accessed using a store-specific provider, such as SQLClient. Entity Framework introduces EntityClient, which allows for entity-level queries against the EDM and a new Object Services layer allowing developers to interact (CRUD) with Entities, as CLR objects, through LINQ to Entities extensions to VB 9.0 or C# 3.0, or a new extension to SQL called Entity SQL. EntityClient acts as the bridge between the Entity Framework and the .NET data provider such as SQLClient or Oracle data provider.
EntityClient follows the same pattern as other existing ADO.NET providers, making available EntityConnection, EntityCommand and EntityDataReader, and allowing developers to retrieve Entities directly with Entity SQL queries without using the Object Services layer.
The EDM and Entity Framework allow developers to program against a conceptual domain model rather than directly against a relational database. By separating code and data, developers can write less code when manipulating data.
Elisa Johnson is a community program manager with the SQL Data Programmability team at Microsoft.