Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Binding to Entity Framework Core

  • 3 minutes to read

This tutorial creates a Grid-based WinForms application, generates an Entity Framework (EF) Core model from the nwind database in the Microsoft SQL Express Server, and binds the Data Grid to a data source that implements asynchronous data processing.

#Create a Grid-based WinForms Application

  1. In Visual Studio, go to “File | New | Project” to create a new project. Select DevExpress v24.2 Template Kit and click Next:

    Create a WinForms Project - DevExpress Template Kit

    Tip

    The application targets .NET 8+.

    Refer to the following help topic for information on how to download and install the DevExpress Template Kit: Install DevExpress Template Kit.

    Note

    Use the DevExpress Project Template Gallery to create applications based on templates that target the .NET Framework.

  2. Specify project settings and click Create to run the DevExpress Project Wizard.
  3. Select the WinForms platform. Select Grid-based Application and click Create Project.

#Install EF Core

Install EF Core-related NuGet packages:

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools

Refer to the following topic for more information: Installing Entity Framework Core.

#Generate an EF Core Model

Generate an EF model based on your database. This tutorial generates an EF model from the nwind database stored at the Microsoft SQL Express Server.

Execute the following command in the Package Manager Console (use your connection parameters):

PM> Scaffold-DbContext "Server=.\SQLEXPRESS;Database=nwind;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
  1. Rebuild the project:

    EFCore - Generated Classes

#Bind the Data Grid to Data

  1. Select the DevExpress GridControl and click Data Source Configuration Wizard in the smart tag menu.
  2. Select “Entity Framework (EF) Core”.
  3. Select your EF Core model and click Next:

    Select Data Model - Data Source Configuration Wizard, DevExpress

  4. Select the data binding mode. This tutorial uses Asynchronous Parallel In-Memory Data Processing. In this mode, all data-intensive operations (sorting, grouping, etc.) on in-memory data are performed asynchronously:

    Data Source Types - Data Source Configuration Wizard, DevExpress

  5. Select the “Categories” table:

    Select the Data Table - Data Source Configuration Wizard, DevExpress

The Data Source Configuration Wizard creates a PLinqInstantFeedbackSource component, binds it to the Data Grid, and generates the following boilerplate code:

C#
public partial class Form1 : RibbonForm {
    public Form1() {
        InitializeComponent();

        // This line of code is generated by the Data Source Configuration Wizard
        this.pLinqInstantFeedbackSource1.GetEnumerable += pLinqInstantFeedbackSource1_GetEnumerable;
    }
    // This event is generated by the Data Source Configuration Wizard
    void pLinqInstantFeedbackSource1_GetEnumerable(object sender, DevExpress.Data.PLinq.GetEnumerableEventArgs e) {
        // Instantiate a new DataContext
        Models.NwindContext dataContext = new Models.NwindContext();
        // Assign a queryable source to the PLinqInstantFeedbackSource
        e.Source = dataContext.Categories;
        // Assign the DataContext to the Tag property
        // to dispose of it in the DismissEnumerable event handler
        e.Tag = dataContext;
    }
}

Run the application to see the result.

DevExpress-powered Grid-based Application - EF Core

#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:

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();
        }
    }
}