gpt4 book ai didi

entity-framework-core - Entity Framework 3.0包含无法像在EF Core 2.2中那样在SQL中进行翻译

转载 作者:行者123 更新时间:2023-12-04 22:15:51 26 4
gpt4 key购买 nike

我正在尝试将Web API从.NET Core 2.2迁移到.NET Core 3.0,但我偶然发现了以下内容:

public Dictionary<int, Tag> GetTagMap(IList<int> tagIds = null)
{
var tags = context.Tag.AsNoTracking();
if (tagIds != null)
tags = tags.Where(t => tagIds.Contains(t.TagId));

return tags
.ToList() // explicit client evaluation in 3.0
.ToDictionary(t => t.TagId, t => t);
}

这通常用于生成类似于以下语句的SQL语句:
SELECT TagId, Name FROM Tag WHERE TagId IN (1, 2, 3)

对于正确索引的列和少量的 IN值来说,该方法非常有效。

现在,我收到以下错误,提示不再支持 List<>.Contains转换:

System.InvalidOperationException: 'The LINQ expression 'Where( source: DbSet, predicate: (t) => (Unhandled parameter: __tagIds_0).Contains(t.TagId))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See Client vs. Server Evaluation - EF Core for more information.'



这表明 LINQ queries are no longer evaluated on the client发生了重大变化,但是未在客户端上评估AFAIK Contains

最佳答案

这是一个3.0错误,由#17342: Contains on generic IList/HashSet/ImmutableHashSet will throw exception跟踪。

已经在3.1中修复。解决方法(如果您迫不及待)是例如强制使用Enumerable.Contains

t => tagIds.AsEnumerable().Contains(t.TagId)

或更改变量的类型。

关于entity-framework-core - Entity Framework 3.0包含无法像在EF Core 2.2中那样在SQL中进行翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58690473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com