Documentation Home
MySQL Connector/NET Developer Guide
Related Documentation Download this Manual

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

4.6 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 database sample, which can be downloaded 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.

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 4.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 Generate from database. 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). For additional instructions on creating a connection to a database see Making a Connection.

    Figure 4.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 4.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 4.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 4.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 4.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 4.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.


User Comments
User comments in this section are, as the name implies, provided by MySQL users. The MySQL documentation team is not responsible for, nor do they endorse, any of the information provided here.
  Posted by Jay Jaeger on October 7, 2017
It would be nice if this article identified some pre-requisites:

1) Install MySQL for Visual Studio AND Connector/Net. You need both.

2) I found that I needed to add a <provider> entry in app.config, per https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html (Why this does not happen in a more user-friendly way I don't know).

3) I also had to add a reference to MySql.Data.Entity.EF6 (under Extensions) or it failed silently when I tried to proceed past the "Choose Your Data Connection" step (and may need to add more - I am not done yet).

Visual Studio Community 2017 V15.3.5
  Posted by Jay Jaeger on October 11, 2017
Aaaaaaaaaaand another thing. The code as written will not work, at least under MySQL 5.7 + Visual Studio 2017 14.3.5, .NET Framework 4.7

I had to change

cityBindingSource.DataSource = we.city;

to

cityBindingSource.DataSource = we.cities.ToList();

as worldEntities did not have a property "city" and if one assigned the property "cities" the execution failed. A little searching with Google yielded this solution, which seems to make sense.
Sign Up Login You must be logged in to post a comment.