0% found this document useful (0 votes)
24 views

EF - Basic

The Entity Framework compiles conceptual and storage schemas into client views. Views drive query and update processing in the runtime engine. Queries can be composed using entity SQL, Language-Integrated Query (LINQ) and object query builder methods.

Uploaded by

FluffyGesus
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

EF - Basic

The Entity Framework compiles conceptual and storage schemas into client views. Views drive query and update processing in the runtime engine. Queries can be composed using entity SQL, Language-Integrated Query (LINQ) and object query builder methods.

Uploaded by

FluffyGesus
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Working with Entity Data

Page 1 of 3

2010 Microsoft Corporation. All rights reserved.

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 [ http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnection (VS.90).aspx ] 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 [ http://msdn.microsoft.com/en-us/library/system.data.entityclient (VS.90).aspx ] provider and the underlying .NET Framework data provider, such as System.Data.SqlClient [ http://msdn.microsoft.com/en-us/library/system.data.sqlclient(VS.90).aspx ] . The following diagram illustrates the Entity Framework architecture for accessing data:

Querying Objects
The Entity Framework tools generate a class derived from ObjectContext [ http://msdn.microsoft.com/enus/library/system.data.objects.objectcontext(VS.90).aspx ] 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 [ http://msdn.microsoft.com/en-us/library/bb896375(VS.90).aspx ] method on the entity reference to load the related object data into the object context. You can also specify a query path

http://msdn.microsoft.com/en-us/library/bb399760(VS.90,printer).aspx

27/09/2010

Working with Entity Data

Page 2 of 3

that defines which related objects to load with returned objects. For more information, see Querying Data as Objects (Entity Framework) [ http://msdn.microsoft.com/en-us/library/bb738642(VS.90).aspx ] . The following example from the quickstart [ http://msdn.microsoft.com/en-us/library/bb399182(VS.90).aspx ] 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. Visual Basic Copy Code

' 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")

C#

Copy Code

// 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) [ http://msdn.microsoft.com/enus/library/bb738642(VS.90).aspx ] . 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) [ http://msdn.microsoft.com/en-us/library/bb399203(VS.90).aspx ] .

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 (Entity Framework) [ http://msdn.microsoft.com/en-us/library/bb738470 (VS.90).aspx ] . The following example from the quickstart [ http://msdn.microsoft.com/en-us/library/bb399182(VS.90).aspx ] gets a collection of Course objects related to a Department object and binds the collection to a DataGridView control. Visual Basic Copy Code

' 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

C#

Copy Code

// 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 [ http://msdn.microsoft.com/en-us/library/bb399182 (VS.90).aspx ] , changes in the object context are written to the database. Visual Basic Copy Code

http://msdn.microsoft.com/en-us/library/bb399760(VS.90,printer).aspx

27/09/2010

Working with Entity Data

Page 3 of 3

' Save object changes to the database, display a message, ' and refresh the form. schoolContext.SaveChanges()

C#

Copy Code

// 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) [ http://msdn.microsoft.com/en-us/library/bb738695(VS.90).aspx ] . 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) [ http://msdn.microsoft.com/en-us/library/bb399203(VS.90).aspx ] .

See Also
Concepts LINQ to Entities Examples [ http://msdn.microsoft.com/en-us/library/bb386981(VS.90).aspx ] Entity Data Model Types [ http://msdn.microsoft.com/en-us/library/bb399548(VS.90).aspx ] Entity Data Model Relationships [ http://msdn.microsoft.com/en-us/library/bb399189(VS.90).aspx ] Other Resources Object Services (Entity Framework) [ http://msdn.microsoft.com/en-us/library/bb399603(VS.90).aspx ] Application Scenarios (Entity Framework) [ http://msdn.microsoft.com/en-us/library/bb738689(VS.90).aspx ]

Tags:

Community Content

http://msdn.microsoft.com/en-us/library/bb399760(VS.90,printer).aspx

27/09/2010

You might also like