.net-core - 从 EntityFrameworkCore 2.1 升级并破坏代码时添加的 SQL 'AS' 关键字

标签 .net-core entity-framework-core

我已经做了好几天了,准备放弃了。

我继承了一个使用 2.1 的 .NET Core 项目,包括 EntityFrameworkCore 2.1。

我在代码中有一个 EF 查询,每当我触摸版本号时都会抛出异常(我尝试过 2.2,但甚至尝试过 2.1.11 甚至 2.1.1!)

SqlException:关键字“AS”附近的语法不正确。

所以我决定查看将要运行的语句的控制台日志,我可以看到一个区别,它只在我更新 EntityFrameworkCore 时出现,但在网上找不到任何东西来记录这个或帮助我。

原始查询是这样开始的:

SELECT [WebsiteCategory].[CategoryID], [WebsiteCategory].[WebSiteID], CASE
    WHEN [t].[Name] IS NULL OR ([t].[Name] = N'')
    THEN [WebsiteCategory.Category].[Category] ELSE [t].[Name]

升级后(即使只是升级到 2.1.1)我看到了这个

SELECT [WebsiteCategory].[CategoryID], [WebsiteCategory].[WebSiteID], CASE
    WHEN [t].[Name] AS [Name0] IS NULL OR ([t].[Name] AS [Name0] = N'')
    THEN [WebsiteCategory.Category].[Category] ELSE [t].[Name] AS [Name0]

变化是一个神秘的 AS [Name0] 突然出现!

不幸的是,我的 SQL 不太强大,我只真正需要使用基本的 SELECT 查询,因为我的大部分开发一直在使用 EntityFramework。

有什么想法吗?这阻碍了我们升级到 2.2,我担心这将意味着我们永远不会再次升级 EntityFramework!

编辑:Linq 查询是

return _productRepository.Context.WebSiteCategory
    .GroupJoin(_productRepository.Context.CategoryTranslations
                                         .Where(z => z.Locale == culture.Name),
        WebsiteCategory => WebsiteCategory.CategoryId,
        Translation => Translation.CategoryId,
        (x, y) => new { WebsiteCategory = x, Translation = y })
    .SelectMany(
        xy => xy.Translation.DefaultIfEmpty(),
        (x, Translation) => new { x.WebsiteCategory, Translation })
    .Select(s => new
                {
                    s.WebsiteCategory,
                    s.Translation
                })
                .Where(x => websiteIds.Contains(x.WebsiteCategory.WebSiteId))
                .Select(x => new
                {
                    x.WebsiteCategory.CategoryId,
                    x.WebsiteCategory.WebSiteId,
                    x.WebsiteCategory.Category.Category,
                    x.Translation.Name,
                    x.WebsiteCategory.Category.CategorySeo,
                    x.WebsiteCategory.Category.Order
                })
    .GroupBy(x => new
                {
                    x.CategoryId,
                    x.WebSiteId,
                    x.Category,
                    x.Name,
                    x.CategorySeo,
                    x.Order
                })
    .Select(x => new ProductCategoryDTO
                {
                    CategoryId = x.Key.CategoryId,
                    WebSiteId = x.Key.WebSiteId,
                    Category = string.IsNullOrEmpty(x.Key.Name) ? x.Key.Category : x.Key.Name,
                    CategorySEO = x.Key.CategorySeo,
                    Order = x.Key.Order
                })
    .OrderBy(x => x.Order)
    .ToList();

最佳答案

我能够使用类似的 LINQ 查询形状重现该问题。问题是由表达式引起的

  string.IsNullOrEmpty(x.Key.Name) ? x.Key.Category : x.Key.Name

GroupBy 之后。表达式的实际类型不是必需的,它似乎发生在除属性访问或聚合方法之外的任何表达式中。

显然这是 EF Core 回归错误。我只能说,他们一直致力于查询翻译,当然还有 GroupBy 优化。然而,不幸的是,随着改进,它们引入了回归。在(如果)他们修复它之前,除了寻找解决方法之外,您无能为力。

解决方法是通过预先选择它们或将它们嵌入到 GroupBy 键中来避免在 GroupBy 之后出现此类表达式。例如,将示例查询更改为

.GroupBy(x => new
{
    x.CategoryId,
    x.WebSiteId,
    Category = string.IsNullOrEmpty(x.Name) ? x.Category : x.Name, // <--
    x.Name,
    x.CategorySeo,
    x.Order
})
.Select(x => new ProductCategoryDTO
{
    CategoryId = x.Key.CategoryId,
    WebSiteId = x.Key.WebSiteId,
    Category = x.Key.Category, // <--
    CategorySEO = x.Key.CategorySeo,
    Order = x.Key.Order
})

关于.net-core - 从 EntityFrameworkCore 2.1 升级并破坏代码时添加的 SQL 'AS' 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57102103/

相关文章:

vb.net - EF Core、VB.NET 中的 LINQ 运算符 '='

entity-framework-core - 我可以将接口(interface)用作 Entity Framework 的属性吗?

c# - IAsyncActionFilter 中的 ParameterDescriptor 上的 GetCustomAttributes 丢失

asp.net-core - 如何使用.net core更改ubuntu操作系统中的系统日期时间

.net - 在.NET Core RC2中构建.exe文件

.net-core - Azure Devops - dotnetcore 构建始终失败 - 进程无法启动

c# - 无法合并两个 NET Standard 项目中的 NuGet 包传递依赖项版本

c# - 如何在 EF Core 和 C# 中使用数据库分片"

asp.net-core - 将 Entity Framework 数据库中的所有字符串设为大写

.net - 使用编译时未知的 ViewComponent