EF - Basic
EF - Basic
Page 1 of 3
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
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 ] .
' 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
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