简体   繁体   English

实体框架-查询可为空的列的问题

[英]Entity Framework - Problem with querying nullable column

I have a problem querying data from a table with a nullable tinyint column. 我在从具有可为null的tinyint列的表中查询数据时遇到问题。
The problem seems to be that the query is generated as: 问题似乎是查询生成为:

AND ( CAST( [Extent1].[PositionEffect] AS int) = @p__linq__3)

=> @p__linq__3 = NULL => @ p__linq__3 = NULL

If i run that query manually it doesn't turn up any results. 如果我手动运行该查询,它不会显示任何结果。 However, when I replace the query with: 但是,当我将查询替换为:

AND ([Extent1].[PositionEffect] IS @p__linq__3)

it turns up the expected results. 它带来了预期的结果。
My C# query looks like this: 我的C#查询如下所示:

 context.Allocations.Where(x => ... && x.PositionEffect == (byte?) positionEffect)

So, why is the entity framework generating the incorrect query here and is there any way to fix this? 那么,为什么实体框架会在此处生成错误的查询,并且有什么方法可以解决此问题?

Thanks, 谢谢,

Tom 汤姆

正如Will A指出的那样,这似乎是Entity Framework中的一个已报告错误,并且生成正确查询的解决方法是:

 (positionEffect == null ? x.PositionEffect == null : x.PositionEffect == (byte?)positionEffect)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM