Documentation Home
MySQL Connector/NET Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 1.3Mb
PDF (A4) - 1.3Mb


MySQL Connector/NET Developer Guide  /  Connector/NET Tutorials  /  Tutorial: Using an Entity Framework Entity as a Windows Forms Data Source

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

This tutorial describes how to create a Windows Forms Data Source from an Entity in an Entity Data Model using Microsoft Visual Studio. The steps are:

To perform the steps in this tutorial, first install the world database sample, which you may download from the MySQL Documentation page. You can also find details on how to install the database on the same page.

Creating a New Windows Forms Application

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

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

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

To acquire the latest Entity Framework assembly for MySQL, download the NuGet package.

Adding an Entity Data Model

To add an Entity Data Model to your solution, do the following:

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

    Figure 6.11 Add Entity Data Model

    Content is described in the surrounding text.

  2. You will now see the Entity Data Model Wizard. You will use the wizard to generate the Entity Data Model from the world database sample. Select the icon EF Designer from database (or Generate from database in older versions of Visual Studio). Click Next.

  3. You can now select the localhost(world) connection you made earlier to the database. Select the following items:

    • Yes, include the sensitive data in the connection string.

    • Save entity connection settings in App.config as:

      worldEntities

    If you have not already done so, you can create the new connection at this time by clicking New Connection (see the figure that follows).

    Figure 6.12 Entity Data Model Wizard - Connection

    Content is described in the surrounding text.

    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. Click Next.

  4. The Entity Data Model Wizard connects to the database.

    As the next figure shows, you are then presented with a tree structure of the database. From here you can select the object you would like to include in your model. If you also created Views and Stored Routines, these items 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 6.13 Entity Data Model Wizard - Objects and Settings

    Content is described in the surrounding text.

    Visual Studio generates a model with three tables (city, country, and countrylanguage) and then display it, as the following figure shows.

    Figure 6.14 Entity Data Model Diagram

    Content is described in the surrounding text.

  5. From the Visual Studio main menu, select Build and then 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 and then Add New Data Source. You will be presented with the Data Source Configuration Wizard.

  2. Select the Object icon. Click Next.

  3. Select the object to bind to. Expand the tree as the next figure shows.

    In this tutorial, you will select the city table. After the city table has been selected click Next.

    Figure 6.15 Data Source Configuration Wizard

    Content is described in the surrounding text.

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

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

Using the Data Source in a Windows Form

This step describes 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 as the following figure shows.

    Note

    The Data Grid View control is bound to cityBindingSource, and the Navigator control is bound to cityBindingNavigator.

    Figure 6.16 Data Form Designer

    Content is described in the surrounding text.

  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 the following code to instantiate the Entity Data Model EntityContainer object and retrieve data from the database to populate the control.

    using System.Windows.Forms;
    
    namespace WindowsFormsApplication4
    {
       public partial class Form1 : Form
       {
          worldEntities we;
    
          public Form1()
          {
              InitializeComponent();
          }
     
          private void Form1_Load(object sender, EventArgs e)
          {
              we = new worldEntities();
              cityBindingSource.DataSource = we.city.ToList();
          }
       }
    }
  3. Save and rebuild the solution.

  4. Run the solution. Confirm that the grid is populated (see the next figure for an example) and that you can navigate the database.

    Figure 6.17 The Populated Grid Control

    Content is described in the surrounding text.

Adding Code to Save Changes to the Database

This step explains how to add code that enables 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 confirm that its Enabled property is set to True.

  2. Double-click the save icon in the form toolbar to display its code.

  3. Add the following (or similar) code to ensure that data is saved to the database when a user clicks the save button in the application.

          public Form1()
          {
              InitializeComponent();
          }
     
          private void Form1_Load(object sender, EventArgs e)
          {
              we = new worldEntities();
              cityBindingSource.DataSource = we.city.ToList();
          }
          private void cityBindingNavigatorSaveItem_Click(object sender, EventArgs e)
          {
              we.SaveChanges();
          }
       }
    }
  4. When the code has been added, save the solution and then rebuild it. Run the application and verify that changes made in the grid are saved.