将StringComparison重载用于谓词时,EF Core中的NullReferenceException [英] NullReferenceException in EF Core when using StringComparison overload for predicates

查看:43
本文介绍了将StringComparison重载用于谓词时,EF Core中的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将项目从使用EF转换为使用EF Core.我们有下面的代码行可以使用,但是现在不行了:

We are converting a project from using EF to using EF Core. We have the following line of code which used to work but now does not:

// throws NullReferenceException
var u = db.Users.FirstOrDefault(x => x.PresenterID.Equals(uid, StringComparison.OrdinalIgnoreCase));

但是,如果我们不使用 StringComparison 重载,它将起作用:

However, if we don't use the StringComparison overload, it works:

// this works
var u = db.Users.FirstOrDefault(x => x.PresenterID.Equals(uid));

这是一个大项目,我们希望避免查找和修改所有进行此类比较的代码.为什么这会引发NullReferencException,并且可以在不更改我们的代码的情况下将其避免?这是堆栈跟踪.谢谢.

This is a large project and we would like to avoid finding and modifying all code that does such comparisons. Why does this throw a NullReferencException and can it be avoided without changing our code? Here is the stack trace. Thanks.

在lambda_method(Closure,User)在System.Linq.Enumerable.WhereEnumerableIterator 1.MoveNext()在System.Linq.Enumerable.FirstOrDefault [TSource](IEnumerable 1源)
在Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ResultEnumerable 1.GetEnumerator()在Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.< _TrackEntities> d__17 2.MoveNext()在Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor <代码> 1.EnumeratorExceptionInterceptor.MoveNext()在System.Linq.Enumerable.First [TSource](IEnumerable 1源)处Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<> c__DisplayClass15_1`1.b__0(QueryContextqc)在Our.Project.Service.ContractsFolderService.ValidateContractUsers(BIContractxmlContract,IDataContext db)中C:\ Projects \ Out.Project \ Main \ Service \ ContractsFolderService.cs:line436

at lambda_method(Closure , User ) at System.Linq.Enumerable.WhereEnumerableIterator1.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source)
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ResultEnumerable1.GetEnumerator() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__172.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext() at System.Linq.Enumerable.First[TSource](IEnumerable1 source) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_1`1.b__0(QueryContext qc) at Our.Project.Service.ContractsFolderService.ValidateContractUsers(BIContract xmlContract, IDataContext db) in C:\Projects\Out.Project\Main\Service\ContractsFolderService.cs:line 436

推荐答案

根据此公开问题在EntityFrameworkCore Github存储库上,LINQ to SQL尚不支持重载.请参阅此特定注释,其中提供了有关防止翻译此重载的问题的详细信息:https://github.com/aspnet/EntityFrameworkCore/issues/1222#issuecomment-443116169

According to this open issue on the EntityFrameworkCore Github repo, that overload is not yet supported with LINQ to SQL. See this specific comment which gives some detail on the problem preventing this overload from being translated: https://github.com/aspnet/EntityFrameworkCore/issues/1222#issuecomment-443116169

我猜想发生空引用异常,因为它会拉回所有结果以评估客户端的字符串比较,并且对于其中一个或多个结果, PresenterID 为空.

I'm guessing that the null reference exception is occurring because its pulling back all the results to evaluate your string comparison on the client side, and for one or more of those results PresenterID is null.

这篇关于将StringComparison重载用于谓词时,EF Core中的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆