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
In Visual Studio, go to “File | New | Project” to create a new project. Select DevExpress v24.2 Template Kit and click Next:
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.
- Specify project settings and click Create to run the DevExpress Project Wizard.
- 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
Rebuild the project:
#Bind the Data Grid to Data
- Select the DevExpress
GridControl
and click Data Source Configuration Wizard in the smart tag menu. - Select “Entity Framework (EF) Core”.
Select your EF Core model and click Next:
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:
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:
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:
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();
}
}
}