This documentation is archived and is not being maintained.

Working with Entity Data

The Entity Framework compiles conceptual model and storage model metadata, together with the mappings between the models, 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 a conceptual model.

The Entity Framework builds on top of storage-specific ADO.NET data providers by providing an EntityConnection to an underlying data provider and data source.

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:

Entity Framework Architectural Diagram

Querying Objects

The ADO.NET Entity Data Model Tools generate a class derived from ObjectContext that represents the entity container defined in the conceptual model. The ObjectContext class supports queries against a conceptual model that return entities as objects, as well as creating, updating, and deleting entity objects. The Entity Framework supports object queries against a conceptual model. 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. If the LazyLoadingEnabled property on the ObjectContext class is set to false, referenced objects are not automatically loaded and you must call the Load method on the entity reference to load the related object data into the object context.

If you used the Entity Data Model Designer to generate your object-layer code, the LazyLoadingEnabled property on the ObjectContext class is set to true by default.

You can also specify a query path that defines which related objects to load with returned objects. For more information, see Querying a Conceptual Model.

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 course objects, ordered by name.
var departmentQuery = from d in schoolContext.Departments.Include("Courses")
                      orderby d.Name
                      select d;

For more information, see Querying a Conceptual Model.

You can define a set of models 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 How to: Import a Stored Procedure.

Working with Objects

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.

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.
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.Courses;

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.

For more information, see Creating, Adding, Modifying, and Deleting Objects.

You can define a conceptual model 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 Walkthrough: Mapping an Entity to Stored Procedures.

See Also