You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6下WinForms DevExpress GridControl适配SQL Server的数据源选型

.NET 6下DevExpress GridControl适配SQL Server的Server Mode数据源方案

由于.NET 6已移除System.Data.Linq命名空间,可采用Entity Framework Core + DevExpress专属Server Mode数据源的组合替代原LinqServerModeSource,实现SQL层面自动分页、排序、过滤和分组,完全兼容WinForms平台。

核心替代组件

DevExpress提供两个适配EF Core的Server Mode数据源:

  • EntityServerModeSource:同步模式,行为与原LinqServerModeSource最接近
  • EntityInstantFeedbackSource:异步模式,后台处理查询,适合超大数据量场景,避免UI阻塞

实现步骤

1. 配置EF Core DbContext

先创建对应SQL Server的EF Core上下文:

using Microsoft.EntityFrameworkCore;

public class AppDbContext : DbContext
{
    public DbSet<Person> Persons { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("你的SQL Server连接字符串");
    }
}

// 实体类示例(需与SQL Server表结构匹配)
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    // 其他字段...
}

2. 使用EntityServerModeSource(同步方案)

// 初始化数据源
var entityServerModeSource = new DevExpress.Data.Linq.EntityServerModeSource();
// 指定上下文类型和查询表
entityServerModeSource.ContextType = typeof(AppDbContext);
entityServerModeSource.QueryableSource = typeof(AppDbContext).GetProperty(nameof(AppDbContext.Persons));
// 绑定到GridControl
gridPersons.DataSource = entityServerModeSource;

3. 使用EntityInstantFeedbackSource(异步方案)

var entityInstantFeedbackSource = new DevExpress.Data.Linq.EntityInstantFeedbackSource();
// 必须指定实体主键字段
entityInstantFeedbackSource.KeyExpression = nameof(Person.Id);

// 提供查询数据源
entityInstantFeedbackSource.GetQueryable += (sender, e) =>
{
    var dbContext = new AppDbContext();
    e.QueryableSource = dbContext.Persons;
    e.Tag = dbContext; // 保存上下文用于后续释放
};

// 释放上下文资源
entityInstantFeedbackSource.DismissQueryable += (sender, e) =>
{
    ((AppDbContext)e.Tag).Dispose();
};

// 绑定到GridControl
gridPersons.DataSource = entityInstantFeedbackSource;

必要NuGet依赖

需安装以下包:

  • DevExpress.Win.Grid:GridControl及Server Mode组件
  • Microsoft.EntityFrameworkCore.SqlServer:EF Core SQL Server驱动
  • Microsoft.EntityFrameworkCore:EF Core核心库

内容的提问来源于stack exchange,提问作者Cosmin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 09:55:22