【问题标题】:Entity Framework problem with reducing projection减少投影的实体框架问题
【发布时间】:2021-12-08 21:36:05
【问题描述】:

我一直致力于通过减少查询的投影来提高使用 EF 5.0.11 的 .NET 核心 API 的性能,但我目前遇到以下情况:

我改进了查询的投影,如下所示:

var employeeEmailQuery = context.Employee
                .Where(e => e.Active == true)
                .Select(e => new EmployeeEmailView
                {
                    Name = e.FullName,
                    Email = e.Email
                });

这将选择查询减少到我需要的两列,而不是数据库中 80 多列上的 SELECT *。

在我的数据库中,我也有带有翻译描述的列。它看起来像这样:

我想做的是根据当前的文化选择相关的翻译描述,所以我添加了以下代码:

 var culture = CultureInfo.DefaultThreadCurrentUICulture;
 var employeeEmailQuery = context.Employee
            .Where(e => e.Active == true)
            .Select(e => new EmployeeEmailView
            {
                Name = e.FullName,
                Email = e.Email,
                this.SetDescription(e, culture);
            });

SetDescription 方法检查区域性并选择正确的列以在 EmployeeEmailView 中设置 Description 属性。但是,通过添加此代码,查询现在再次执行 SELECT *,这是我不想要的。

是否有人知道如何使用 EF 动态包含选择列而不将所有内容重写为原始 SQL?

提前致谢。

【问题讨论】:

  • minimal reproducible example 在询问 SQL 问题时是一个很好的开始。
  • 您使用的是哪个 dbms?
  • @jarlh 我使用的是 SQL Server 2014

标签: sql sql-server entity-framework entity-framework-5


【解决方案1】:

我认为唯一的方法是使用Interceptor 来修改查询,或者使用表达式动态生成 EF IQueryable。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多