Skip to main content

Binding to Entity Framework Core

  • 4 minutes to read

Entity Framework (EF) Core is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology. To bind a DevExpress data-aware control to an EF Core source, you need to manually create a Data Model first. Then, utilize the Data Source Configuration Wizard.

Database and Data Model

Depending on the database provider you choose, you may be required to execute different steps and install different NuGet packages. Below are a few Microsoft tutorials on creating code-first Models or EF Models based on existing databases.

The example below illustrates how to generate an EF model from a sample Adventure Works 2014 database stored at the SQL Express server.

  1. Create a new WinForms solution using the DevExpress Template Gallery. Choose the “Grid based UI” template to create a Data Grid control hosted within a skinnable XtraForm.
  2. Make sure the target .NET Framework of your project is version 4.5 or higher. To check and modify the target framework version, go to “Project | <Your_Project_Name> Properties”, then use the “Target framework” editor to select the required version.

    OData - Framework

  3. For Entity Framework Core 2.0, perform additional steps before proceeding.

    • Make sure you have Visual Studio 2017 (version 15.3.1 and newer) installed - older VS versions are not supported.
    • Install .NET Core SDK.
    • Open Visual Studio settings (“Debug | Options”), switch to the “NuGet Package Manager | General” tab and choose “PackageReference” as the default package management format.

      EFCore - Packages

      Alternatively, you can add the following line to your .csproj/.vbproj file.

      <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
      
    • Enable automatic binding redirect generation by adding the following line to the .csproj/.vbproj file.

      <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
      
  4. Open the PMC (“Tools | NuGet Package | Package Manager Console”) and enter the following lines one-by-one to install the required packages.

    • Microsoft SQL Server database provider

      PM > 

      Install-Package Microsoft.EntityFrameworkCore.SqlServer

    • Entity Framework Core Package Manager Console Tools

      PM > 

      Install-Package Microsoft.EntityFrameworkCore.Tools

    Tip

    Refer to the Database Providers article to learn how to install NuGet packages for other providers.

  5. In PMC, enter the following line to generate an EF model based on your database. Note that you must replace the connection parameters (Server, Database and login credentials) with your actual values.

    PM > 

    Scaffold-DbContext “Server=MYSERVER\SQLEXPRESS;Database=AdventureWorks2014;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer

    This will add multiple class files to your solution. The “<Database_Name>Context” class represents a session with the database and allows you to query and save instances of the entity classes. Other files represent individual database tables.

    EFCore - Generated Classes

  6. Rebuild your project.

Data Binding

  1. Invoke the Data Source Configuration Wizard for the Data Grid control and select “Entity Framework (EF) Core”. If the Data Model was generated correctly, you will see it under the “Data Sources” header. Select it and click “Next” to proceed.

    EFCore - Wizard Page 1

  2. Select the required connection type.

    EFCore - Wizard Page 2

    The last two options enable Server and Instant Feedback Mode modes. Selecting these modes will result in adding the PLinqServerModeSource or PLinqInstantFeedbackSource components respectively.

  3. Select the required database table. For all connection types except for direct binding, you can also apply sorting by the selected data field.

    EFCore - Wizard Page 3

  4. Run the application to see the result.

    EFCore - Runtime

Bind Data Editors to Source Object Properties

The following code sample uses the DataNavigator to switch between data source records and post changes to the database:

Bind ExtraEditors to Entity Framework Core

using Microsoft.EntityFrameworkCore;
using System;
using ExtraEditors_EFCore.Models;

namespace ExtraEditors_EFCore {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        // ...
        NorthwindEntities northwindDBContext;

        private void Form1_Load(object sender, EventArgs e) {
            northwindDBContext = new NorthwindEntities();
            northwindDBContext.Orders.Load();
            northwindDBContext.Shippers.Load();

            ordersSource.DataSource = northwindDBContext.Orders.Local.ToBindingList();
            shippersSource.DataSource = northwindDBContext.Shippers.Local.ToBindingList();

            lookUpEdit1.Properties.DataSource = shippersSource;
            lookUpEdit1.Properties.DisplayMember = "CompanyName";
            lookUpEdit1.Properties.ValueMember = "ShipperId";

            dataNavigator1.DataSource = ordersSource;

            textEdit1.DataBindings.Add("EditValue", ordersSource, "OrderId");
            textEdit2.DataBindings.Add("EditValue", ordersSource, "ShipName");
            dateEdit1.DataBindings.Add("EditValue", ordersSource, "OrderDate");
            lookUpEdit1.DataBindings.Add("EditValue", ordersSource, "ShipVia");
        }

        private void dataNavigator1_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e) {
            if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.EndEdit)
                northwindDBContext.SaveChanges();
        }
    }
}