Queryable.Any()返回null? [英] Queryable.Any() returning null?
问题描述
我有一个数据库查询,如:
I have a database lookup like:
var configs = dbData.Configs.Where(e => headers.Contains(e.headerId) && e.flag == "true");
if(configs.Any())
{ ... }
其中,CONFIGS返回为一个IQueryable类型。我有时会收到此错误:
where configs is returned as an IQueryable type. I sometimes get this error:
System.InvalidOperationException:演员到值类型'布尔'
失败,因为物化价值空值。无论结果
型的泛型参数或查询必须使用可空类型。在在
System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader 1.GetValue(DbDataReader
1.ReadNextElement(整形
读卡器,有序的Int32)System.Data.Common.Internal .Materialization.Shaper.GetColumnValueWithErrorHandling在
System.Data.Common.Internal.Materialization.Coordinator [TColumn](的Int32
序)
成型机)在
System.Data.Common.Internal.Materialization.Shaper 1.SimpleEnumerator.MoveNext()
在System.Linq.Enumerable.Single [TSource](IEnumerable的$在$ b code> 1源)b System.Linq.Queryable.Any [TSource](IQueryable`1源)
System.InvalidOperationException: The cast to value type 'Boolean' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type. at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader
1.GetValue(DbDataReader reader, Int32 ordinal) at System.Data.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling[TColumn](Int32 ordinal) at System.Data.Common.Internal.Materialization.Coordinator
1.ReadNextElement(Shaper shaper) at System.Data.Common.Internal.Materialization.Shaper1.SimpleEnumerator.MoveNext() at System.Linq.Enumerable.Single[TSource](IEnumerable
1 source) at System.Linq.Queryable.Any[TSource](IQueryable`1 source)
它发生在生产,看似随意,我一直无法重现它在发展
It happens in production, seemingly randomly and I haven't been able to reproduce it in development.
编辑补充:该配置类型都有一个外键约束,如果布尔类型正在从投,我不知道。有一个在由实体框架创建的配置类型没有布尔也没有布尔类型在SQL服务器反正。
EDIT TO ADD: The Config type has a foreign key constraint, if the Boolean type is being cast from that, I don't know. There is no Boolean in the Config type created by Entity Framework and there is no Boolean type in Sql Server anyway.
推荐答案
您有在你的Configs表中的一些数据库列有一个可空布尔类型,但你映射表项的C#实体被定义为使用非空的布尔值。当C#实体正在被查询提供商,填充当它运行到空
值,它会抛出这个异常。您需要更新的实体对象,这样有问题的列是正确的类型。
You have some database column in your Configs table that has a nullable boolean type, but the C# entity you are mapping the table entry to is defined as using a non-nullable boolean. When the C# entity is being populated by the query provider, when it runs into a null
value, it throws this exception. You need to update the entity object so that the column in question is of the proper type.
这篇关于Queryable.Any()返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!