5.7 Tutorial: Using an Entity Framework Entity as a Windows Forms Data Source

In this tutorial you will learn how to create a Windows Forms Data Source from an Entity in an Entity Data Model. This tutorial assumes that you have installed the World example database, which can be downloaded from the MySQL Documentation page. You can also find details on how to install the database on the same page. It will also be convenient for you to create a connection to the World database after it is installed. For instructions on how to do this see Section 4.2, “Making a Connection”.

Creating a new Windows Forms application

The first step is to create a new Windows Forms application.

  1. In Visual Studio, select File, New, Project from the main menu.

  2. Choose the Windows Forms Application installed template. Click OK. The solution is created.

Adding an Entity Data Model

You will now add an Entity Data Model to your solution.

  1. In the Solution Explorer, right-click your application and select Add, New Item.... From Visual Studio installed templates select ADO.NET Entity Data Model. Click Add.

    Figure 5.23 Add Entity Data Model

    Add Entity Data Model

  2. You will now see the Entity Data Model Wizard. You will use the wizard to generate the Entity Data Model from the world example database. Select the icon Generate from database. Click Next.

    Figure 5.24 Entity Data Model Wizard Screen 1

    Entity Data Model Wizard Screen 1

  3. You can now select the connection you made earlier to the World database. If you have not already done so, you can create the new connection at this time by clicking New Connection.... For further instructions on creating a connection to a database see Section 4.2, “Making a Connection”.

    Figure 5.25 Entity Data Model Wizard Screen 2

    Entity Data Model Wizard Screen 2

  4. Make a note of the entity connection settings to be used in App.Config, as these will be used later to write the necessary control code.

  5. Click Next.

  6. The Entity Data Model Wizard connects to the database. You are then presented with a tree structure of the database. From this you can select the object you would like to include in your model. If you had created Views and Stored Routines these will be displayed along with any tables. In this example you just need to select the tables. Click Finish to create the model and exit the wizard.

    Figure 5.26 Entity Data Model Wizard Screen 3

    Entity Data Model Wizard Screen 3

  7. Visual Studio will generate the model and then display it.

    Figure 5.27 Entity Data Model Diagram

    Entity Data Model Diagram

  8. From the Visual Studio main menu select Build, Build Solution, to ensure that everything compiles correctly so far.

Adding a new Data Source

You will now add a new Data Source to your project and see how it can be used to read and write to the database.

  1. From the Visual Studio main menu select Data, Add New Data Source.... You will be presented with the Data Source Configuration Wizard.

    Figure 5.28 Entity Data Source Configuration Wizard Screen 1

    Entity Data Source Configuration Wizard Screen 1

  2. Select the Object icon. Click Next.

  3. You will now select the Object to bind to. Expand the tree. In this tutorial, you will select the city table. Once the city table has been selected click Next.

    Figure 5.29 Entity Data Source Configuration Wizard Screen 2

    Entity Data Source Configuration Wizard Screen 2

  4. The wizard will confirm that the city object is to be added. Click Finish.

    Figure 5.30 Entity Data Source Configuration Wizard Screen 3

    Entity Data Source Configuration Wizard Screen 3

  5. The city object will be display in the Data Sources panel. If the Data Sources panel is not displayed, select Data, Show Data Sources from the Visual Studio main menu. The docked panel will then be displayed.

    Figure 5.31 Data Sources

    Data Sources

Using the Data Source in a Windows Form

You will now learn how to use the Data Source in a Windows Form.

  1. In the Data Sources panel select the Data Source you just created and drag and drop it onto the Form Designer. By default the Data Source object will be added as a Data Grid View control. Note that the Data Grid View control is bound to the cityBindingSource and the Navigator control is bound to cityBindingNavigator.

    Figure 5.32 Data Form Designer

    Data Form Designer

  2. Save and rebuild the solution before continuing.

Adding Code to Populate the Data Grid View

You are now ready to add code to ensure that the Data Grid View control will be populated with data from the City database table.

  1. Double-click the form to access its code.

  2. Add code to instantiate the Entity Data Model's EntityContainer object and retrieve data from the database to populate the control.

    Figure 5.33 Adding Code to the Form

    Adding Code to the Form

  3. Save and rebuild the solution.

  4. Run the solution. Ensure the grid is populated and you can navigate the database.

    Figure 5.34 The Populated Grid Control

    The Populated Grid Control

Adding Code to Save Changes to the Database

You will now add code to enable you to save changes to the database.

The Binding source component ensures that changes made in the Data Grid View control are also made to the Entity classes bound to it. However, that data needs to be saved back from the entities to the database itself. This can be achieved by the enabling of the Save button in the Navigator control, and the addition of some code.

  1. In the Form Designer, click the Save icon in the Form toolbar and ensure that its Enabled property is set to True.

    Figure 5.35 Save Button Enabled

    Save Button Enabled

  2. Double-click the Save icon in the Form toolbar to display its code.

  3. You now need to add code to ensure that data is saved to the database when the save button is clicked in the application.

    Figure 5.36 Adding Save Code to the Form

    Adding Save Code to the Form

  4. Once the code has been added, save the solution and rebuild it. Run the application and verify that changes made in the grid are saved.