Export (0) Print
Expand All

AdventureWorks Data Binding (EDM Sample Application)

.NET Framework 3.5

The AdventureWorks Data Binding sample demonstrates data binding that uses the Entity Framework. The data model is defined in the topic AdventureWorks Sales Model (EDM). This application displays and modifies SalesOrderDetail objects associated with a selected SalesOrderHeader object. A user selects from a list of available products when adding a new SalesOrderDetail to an order. For more information about how to bind entity objects to controls by using the Entity Framework, see Binding Objects to Controls (Entity Framework).

Downloading and Running the Sample

The AdventureWorks Data Binding sample is available on the ADO.NET Entity Framework Documentation Samples resource page in MSDN Code Gallery. Requirements and instructions for building and running the sample are provided in the readme.htm file in the download package.

Entity Framework Features

This sample uses the following features of the Entity Framework.

Entity Data Model (EDM)

The sample uses the AdventureWorks Sales Model. For more information, see AdventureWorks Sales Model (EDM).

Data binding

Data sources are defined for each entity type that is bound to a Windows Forms control. These data sources enable data bindings to be defined in Visual Studio. At run time, changes to data-bound controls are automatically reflected in the bound data objects. For more information, see How to: Add an Object as a Project Data Source (Entity Framework).

Query builder methods

Query builder methods are methods of ObjectQuery that can be used to sequentially construct query commands that are the same as Entity SQL. In this sample, the Where method returns the specified SalesOrderHeader object, and the Include method returns related SalesOrderDetail objects. For more information, see Query Builder Methods (Entity Framework).

Property validation

Validation of property changes on the SalesOrderDetail entity type is performed in the objects themselves by implementing the OnPropertyChanging and the OnPropertyChanged partial methods. This is a simpler approach than the alternative of handling various events that the data bound controls raise. For example, in the OnOrderQtyChanging partial method, when the quantity is less than one, validation fails and an exception is raised. This exception is handled automatically by the bound control. For a DataGridView object, the DataError event is handled for validation errors. For more information, see How to: Execute Business Logic During Property Changes (Entity Framework).

Partial methods

Partial methods on the SalesOrderHeader and SalesOrderDetail classes are used to calculate the LineTotal, SubTotal, and TotalDue properties on the client. These properties represent computed columns in the database. Without this logic, these properties are not updated on the client until SaveChanges is called. For more information, see Customizing Objects (Entity Framework).

Long-running ObjectContext

Because the Windows Forms contain controls that are bound to data in the ObjectContext, the context must be persisted throughout the duration of the forms. This ObjectContext instance is passed to the FormAddSalesOrderDetail form. Object Services automatically opens and closes the connection as needed. When the form is closed, the context is explicitly disposed of. For more information, see Managing Resources in Object Services (Entity Framework).

See Also

Community Additions

© 2015 Microsoft