Querying Entities and Associations

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

In this task, you will create strongly-typed queries against the CLR objects that represent entities and associations in the School model, and bind display controls to the object collections returned from these queries.

  1. At the beginning of the code file for the CourseViewer form, add the following using (C#) or Imports (Visual Basic) statements to reference the model created from the School database and the entity namespace.

  2. At the top of the partial class definition for the CourseViewer form, add the following code that creates an ObjectContext instance.

  3. In the CourseViewer form designer, double-click the CourseViewer form.

    This opens the code page for the form and creates the courseViewer _Load event handler method.

  4. In the courseViewer _Load event handler method, copy and paste the following code that defines the DataGridView, executes a query that returns a collection of departments (ordered by Name), and binds the collection of Department objects to the departmentList control.

  1. In the CourseViewer form designer, double-click the departmentList control.

    This creates the departmentList_SelectedIndexChanged event handler method.

  2. Paste the following code that loads the courses that are related to the selected department.

You have successfully created queries that return Department and Course objects and bound those objects to controls. Next, you will save changes made to Course objects in the data grid back to the database: Inserting and Updating Data.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft