How to: Add an Object as a Project Data Source (Entity Framework)
You can create data sources that are based on your objects in a Visual Studio application. After you define an entity type as a data source in a project, you can create forms that display Entity Data Model (EDM) data by dragging items from the Data Sources window onto forms. These items become controls on the form that are bound to the data source. For more information, see Binding Objects to Controls (Entity Framework).
In this topic, you will create a data source for the SalesOrderHeader type in the Adventure Works Sales Model. You will then use this data source to create a Windows form with controls that are bound to entity data. To complete these procedures, you must have already added the AdventureWorks Sales model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Use the Entity Data Model Wizard (Entity Framework).
To create a data source based on the SalesOrderHeader types
If you have recently added the Entity Data Model (EDM), build the project.
On the Data menu, click Add New Data Source.
On the Choose a Data Source Type page, select Object.
On the Select an Object You Wish to Bind to page, expand the project node, expand the project's namespace node, and select the SalesOrderHeader type in the tree view.
Click Finish.
The SalesOrderHeader data source is added to the Data Sources window.
To add data source bound controls to a Windows form
In the Data Sources window, expand the SalesOrderHeader node.
Drag one or more properties from the SalesOrderHeader node to the form.
This creates the salesOrderHeaderBindingSource and salesOrderHeaderBindingNavigator controls on the form. A data-bound control is also created on the form for each property, accompanied by an appropriately titled label control.
Drag the SalesOrderDetail navigation property to the form.
This creates the salesOrderDetailBindingSource control with the DataSource property of the control set to the salesOrderHeaderBindingSource and the DataMember property set to SalesOrderDetail. It also creates the salesOrderDetailDataGridView data-bound control on the form, accompanied by an appropriately titled label control.
To bind the data source to the result of an object query
Open the code page for the form and add the following using statements (Imports in Visual Basic):
Imports System.Data.Objects
using System.Data.Objects;
In the partial class that defines the form, add the following code that creates an ObjectContext instance and defines the customerID constant.
Dim context As AdventureWorksEntities Const customerId As Integer = 277
private AdventureWorksEntities context; private const int customerId = 277;
In the form designer, double-click the form.
This opens the code page for the form and creates the method that handles the Load event for the form.
In the Load event handler, copy and paste the following code.
' Initialize the object context. context = New AdventureWorksEntities() Try ' Create a query for orders and related items. Dim orderQuery As ObjectQuery(Of SalesOrderHeader) = _ context.SalesOrderHeader _ .Where("it.CustomerID = @customerId", _ New ObjectParameter("customerId", customerId)) _ .Include("SalesOrderDetail") ' Set the data source of the binding source to the ObjectResult ' returned when the query is executed. SalesOrderHeaderBindingSource.DataSource = _ orderQuery.Execute(MergeOption.AppendOnly) Catch ex As EntitySqlException MsgBox(ex.Message) End Try
// Initialize the object context. context = new AdventureWorksEntities(); try { // Create a query for orders and related items. ObjectQuery<SalesOrderHeader> orderQuery = context.SalesOrderHeader .Where("it.CustomerID = @customerId", new ObjectParameter("customerId", customerId)) .Include("SalesOrderDetail"); // Set the data source of the binding source to the ObjectResult // returned when the query is executed. salesOrderHeaderBindingSource.DataSource = orderQuery.Execute(MergeOption.AppendOnly); } catch (EntitySqlException ex) { MessageBox.Show(ex.Message); }
This code executes a query that returns a collection of SalesOrderHeader and related SalesOrderDetail objects for a specific customer and binds the collection of SalesOrderHeader objects to the salesOrderHeaderBindingSource.
See Also
Tasks
How to: Bind Objects to Windows Presentation Foundation Controls (Entity Framework)
How to: Bind Objects to Windows Form Controls (Entity Framework)
Concepts
Binding Objects to Controls (Entity Framework)
Bind Entity Data to Controls (Application Scenarios)