Working with Entity Data
The Entity Framework compiles a set of conceptual and storage schemas, together with the mappings between them, into bidirectional pairs of Entity SQL statements called client views. These views drive query and update processing in the runtime engine. The mapping compiler that generates the views can be invoked either at design time or at runtime when the first query is executed against an Entity Data Model (EDM) schema.
The Entity Framework builds on top of storage-specific ADO.NET data providers by providing an EntityConnection to an underlying data provider and relational database.
When a query is executed, it is parsed and converted into a canonical command tree, which is an object model representation of the query. Canonical command trees represent select, update, insert, and delete commands. All subsequent processing is performed on the command tree, which is the means of communication between the System.Data.EntityClient provider and the underlying .NET Framework data provider, such as System.Data.SqlClient.
The following diagram illustrates the Entity Framework architecture for accessing data:
The Entity Framework tools generate a class derived from ObjectContext that represents the entity container defined in the conceptual model. The ObjectContext class supports queries against an EDM that return entities as objects, as well as creating, updating, and deleting entity objects. The Entity Framework supports object queries against an EDM. Queries can be composed using Entity SQL, Language-Integrated Query (LINQ), and object query builder methods.
In a conceptual model, entities are related to each other by associations. In the object layer, these associations are represented by properties that expose collections of related objects based on an entity reference. For example, in the School model, Department.Course gets an entity collection of Course objects based on the association between Course and Department. Because referenced objects are not automatically loaded, you must call the Load method on the entity reference to load the related object data into the object context. You can also specify a query path that defines which related objects to load with returned objects. For more information, see Querying Data as Objects (Entity Framework).
The following example from the quickstart shows a query that, when executed, retrieves all Department objects. A query path definition ensures that the Course objects related to Department objects are also returned. An Entity SQL ORDER BY clause orders the returned objects by Name.
' Define a query that returns all Department objects and related
' Course objects, ordered by name.
Dim departmentQuery As ObjectQuery(Of Department) = _
schoolContext.Department.Include("Course").OrderBy("it.Name")
// Define a query that returns all Department objects and related
// Course objects, ordered by name.
ObjectQuery<Department> departmentQuery =
schoolContext.Department.Include("Course").OrderBy("it.Name");
For more information, see Querying Data as Objects (Entity Framework).
You can define an EDM that uses stored procedures to execute queries at the data source. The result sets from these stored procedures are mapped to entities in the conceptual model. For more information, see Stored Procedure Support (Entity Framework).
An object in an object context is an entity type representation of data in the data source. You can modify, create, and delete objects in an object context. The object context manages identities and relationships between objects. You can also serialize objects and bind objects to controls. For more information, see Working with Objects (Entity Framework).
The following example from the quickstart gets a collection of Course objects related to a Department object and binds the collection to a DataGridView control.
' Get the object for the selected department.
Dim department As Department = _
CType(Me.departmentList.SelectedItem, Department)
' Bind the grid view to the collection of Course objects
' that are related to the selected Department object.
courseGridView.DataSource = department.Course
// Get the object for the selected department.
Department department =
(Department)this.departmentList.SelectedItem;
// Bind the grid view to the collection of Course objects
// that are related to the selected Department object.
courseGridView.DataSource = department.Course;
The Entity Framework tracks changes to entity data and enables you to persist changes back to the data source. In the following example from the quickstart, changes in the object context are written to the database.
' Save object changes to the database, display a message,
' and refresh the form.
schoolContext.SaveChanges()
// Save object changes to the database, display a message,
// and refresh the form.
schoolContext.SaveChanges();
For more information, see Adding, Modifying, and Deleting Objects (Entity Framework).
You can define an EDM that uses stored procedures to insert, update, and delete data at the data source. These stored procedures are mapped to entities in the conceptual model. For more information, see Stored Procedure Support (Entity Framework).
LINQ to Entities Examples
Entity Data Model Types
Entity Data Model Relationships
Object Services (Entity Framework)
Application Scenarios (Entity Framework)