Back to Devexpress

Binding to Entity Framework Core

windowsforms-118049-common-features-data-binding-binding-to-entity-framework-core.md

latest6.8 KB
Original Source

Binding to Entity Framework Core

  • Jan 28, 2025
  • 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 v25.2 Template Kit and click Next :

  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:

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 :

  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:

  5. Select the “Categories” table:

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

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

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:

csharp
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();
        }
    }
}
vb
Imports Microsoft.EntityFrameworkCore
Imports System
Imports ExtraEditors_EFCore.Models

Namespace ExtraEditors_EFCore
    Public Partial Class Form1
        Inherits DevExpress.XtraEditors.XtraForm
        ' ...
        Private northwindDBContext As NorthwindEntities

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            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")
        End Sub

        Private Sub dataNavigator1_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.NavigatorButtonClickEventArgs)
            If e.Button.ButtonType = DevExpress.XtraEditors.NavigatorButtonType.EndEdit Then northwindDBContext.SaveChanges()
        End Sub
    End Class
End Namespace