【发布时间】:2021-10-13 11:33:11
【问题描述】:
我有一段代码在 EF Core 2.2 中工作,用于比较字符串大小写,如下所示。
public async Task<bool> DoesItemNumberExists(Guid revisionId, string itemNumber)
{
var doesExist = await _repository.AnyAsync(a => string.Equals(a.ItemNo, itemNumber, StringComparison.Ordinal) && a.SoqHeading_NP.SoqRevisionId == revisionId);
return doesExist;
}
我在 EF Core 5 中运行相同的代码,但应用程序崩溃了。有什么帮助吗?
以下是我得到的异常
The LINQ expression 'DbSet<SoqItem>()
.Where(s => s.IsDeleted == False)
.Join(
inner: DbSet<SoqHeading>()
.Where(s0 => s0.SoqRevisionId == __ef_filter__RevisionId_0 && s0.IsDeleted == False),
outerKeySelector: s => EF.Property<Nullable<Guid>>(s, "SoqHeadingId"),
innerKeySelector: s0 => EF.Property<Nullable<Guid>>(s0, "Id"),
resultSelector: (o, i) => new TransparentIdentifier<SoqItem, SoqHeading>(
Outer = o,
Inner = i
))
.Any(s => string.Equals(
a: s.Outer.ItemNo,
b: __itemNumber_0,
comparisonType: Ordinal) && s.Inner.SoqRevisionId == __revisionId_1)' could not be translated. Additional information: Translation of the 'string.Equals' overload with a 'StringComparison' parameter is not supported. See https://go.microsoft.com/fwlink/?linkid=2129535 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
【问题讨论】:
-
导致应用崩溃的异常是什么?
-
学习要点:请永远不要再发布关于 SO 的问题,该问题声明、暗示甚至暗示您遇到了异常,但不包括完整、准确的异常类型和消息。我真的不敢相信我每天看到的有多少人说“我出错了”,但没有再提任何关于它的事情(就像这是一些无关紧要、无关紧要的事情/无论如何我们都会立即确切地知道它是什么);这是我们可以拥有的调试中最有用的一条信息
-
请阅读错误信息?它是更好的之一。. 让我们知道您对此不了解的地方,以便我们指导您的学习 - 如果我们只是说“这样做,它会解决它”,那么您将不会学到太多
-
@thanzeel 这不是 EF Core 5 错误。查询始终无法转换为 SQL,但 EF Core 2 通过将 everything 加载到内存中然后匹配客户端上的记录而不使用索引来解决此问题。 EF Core 3 禁用了这种极其不幸的行为。如果您想避免对性能造成巨大影响不要尝试强制大小写匹配。确保列的排序规则与您想要的匹配并在其上创建索引。在查询中只使用
== -
@thanzeel 如果您希望为不同的查询同时区分大小写和 in 匹配,则必须使用不同的排序规则和索引创建不同的列(可能已计算)那也是。索引受排序规则的影响,因为这显然会影响列值的相等性和顺序。
标签: c# asp.net-core entity-framework-core