10bet网址
Connectors and APIs Manual
Download this Manual

Connectors and APIs Manual/.../ Tutorial: Using an Entity Framework Entity as a Windows Forms Data Source

4.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 theworlddatabase sample, which you may download from theMySQL 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, selectFile,New, and thenProjectfrom the main menu.

  2. Choose theWindows Forms Applicationinstalled template. ClickOK. The solution is created.

获取最新的实体框架组装for MySQL, download the NuGet package. Alternatively, use the MySQL Application Configuration tool provided by MySQL for Visual Studio 1.2.9 (or later) to acquire the latest package and coordinate the configuration. For more information about using the tool, seeEntity Framework.

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 selectAddand thenNew Item. FromVisual Studio installed templates, selectADO.NET Entity Data Model(see the figure that follows). ClickAdd.

    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 theworlddatabase sample. Select the iconEF Designer from database(orGenerate from databasein older versions of Visual Studio). ClickNext.

  3. You can now select thelocalhost(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 inApp.configas:

      worldEntities

    If you have not already done so, you can create the new connection at this time by clickingNew Connection(see the figure that follows). For additional instructions on creating a connection to a database seeMaking 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 inApp.Config, as these will be used later to write the necessary control code. ClickNext.

  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. ClickFinishto 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, selectBuildand thenBuild Solutionto 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 selectDataand thenAdd New Data Source. You will be presented with the Data Source Configuration Wizard.

  2. Select theObjecticon. ClickNext.

  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 clickNext.

    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. ClickFinish.

  5. The city object will now appear in the Data Sources panel. If the Data Sources panel is not displayed, selectDataand thenShow Data Sourcesfrom 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 tocityBindingSource, and the Navigator control is bound tocityBindingNavigator.

    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 ModelEntityContainerobject 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 itsEnabledproperty is set toTrue.

  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.

    公共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.