Drag-and-Drop Data Binding
Before digging into the code, rename the default EntitySet for the Customer
entity. The wizard gives both the entity and the EntitySet name of the table. This is not optimal for an EDM where the entity is a single thing and the EntitySet is a collection of things. Fix the Customer
Entity so that coding against it will be more intuitive. Open up the model in the designer; click the Customer
entity, and then in the Properties window for the Customer Entity, change "Name" to "Customer."
Next, create an Object Data Source in the same way that you would create one from any other class type. If you do not already have the Data Sources window open, open it by choosing Show Data Sources from the Data option of Visual Studio's menu.
Add a new data source, which will open up the Data Source Configuration Wizard. Select Object as the Data Source type and click Next. On the next screen, you will see all the classes that Visual Studio found in the solution. Open the tree node for the model's namespace to see the various classes that the wizard created based on the database tables. As shown in Figure 5
, select one of the tables from which to create the Object Data Source, for example, Customers
|Figure 5: Creating an Object Data Source from classes generated from the Entity Data Model. You can then use the Object Data Source for data binding in Windows Forms applications.|
The beauty of this process is that the ADO.NET Entity Framework plugs right into your existing functionality. Other than running the Entity Data Model wizard, none of the steps above are any different than you're used to with Visual Studio 2005.
Continuing on the familiar data binding path, drag the Customers object from the Data Sources window onto the form. This will result in a DataGridView bound to the Customer object and a navigation toolbar.
Copy the following code into the form's code to access the Customers and bind them to the database. Notice that there is no data access code being written here. All you need to do is get a reference to the Entities wrapper class, ask it to hand you the customers, and then bind the results to the BindingSource, which was bound to the grid in the drag-and-drop operation. The NWEntities context will keep track of any changes made to its objects (the customer entities) through the grid. To simplify the code even more, I am using implicitly typed local variables, a new language feature for C# 3.0 and Visual Basic 9.0, that lets you use variables without predefining their types:
'Declare the wrapper class to the entities
Dim NWEntities As NorthwindModel.NWEntities
Private Sub Form1_Load(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
NWEntities = New NorthwindModel.NWEntities
'Fetch the customers from the Entities class
Dim custs = NWEntities.Customers
'Bind the custs object to the BindingSource.
CustomersBindingSource.DataSource = custs
Run the application and you can see that this minimal code populates the view with the customer data.
Update the Data
Although change tracking is happening, you haven't had to write any code to explicitly update changes to the database. To make the updates happen, be sure that you have enabled the Save button on the navigator toolbar. You can do this by selecting the button and changing its Enabled
property in the Property window to True
. Double-click the Save button in the designer to get to its Click
event handler. Add the following code to handle the data update:
Frighteningly simple—but honestly, that's all you need because the ADO.NET Entity Framework performs the change tracking for you. Performance is good because this method only updates changed rows. It's also important to note that, by default, the ADO.NET Entity Framework uses Optimistic Concurrency for updates—although you can modify the setting.