我有一个数据库查找,例如:
var configs = dbData.Configs.Where(e => headers.Contains(e.headerId) && e.flag == "true");
if(configs.Any())
{ ... }
其中 configs 作为 IQueryable 类型返回。我有时会收到此错误:
System.InvalidOperationException:转换为值类型“Boolean”
失败,因为具体化值为 null。要么是结果
type 的泛型参数或查询必须使用可为 null 的类型。在
System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader1.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(整形器
塑造者)在
System.Data.Common.Internal.Materialization.Shaper1.SimpleEnumerator.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable
1 个来源)位于
System.Linq.Queryable.Any[TSource](IQueryable`1 源)
它在生产中发生,看似随机,我无法在开发中重现它。
编辑添加: Config 类型有一个外键约束,如果布尔类型是从中转换的,我不知道。 Entity Framework 创建的 Config 类型中没有 Boolean,Sql Server 中也没有 Boolean 类型。
您的 Configs 表中有一些数据库列具有可为空的布尔类型,但您将表条目映射到的 C# 实体被定义为使用不可为空的布尔值。当 C# 实体由查询提供程序填充时,当它遇到null
值,它会抛出此异常。您需要更新实体对象,以便相关列具有正确的类型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)