nHibernate无法将布尔值转换为字符串 [英] nHibernate unable to cast Boolean to String
问题描述
我有以下查询:
var query = from item in Session.Query<FactuurItem>()
where item.EnergieType == etype
&& (item.DienstType == null || item.DienstType == DienstType.Onbekend || item.DienstType == dtype)
&& item.IsActive == true
orderby item.Naam
select item;
将转换为以下SQL:
select * from [FactuurItem] factuurite0_
where
factuurite0_.EnergieType=?
and (factuurite0_.DienstType is null or factuurite0_.DienstType=? or factuurite0_.DienstType=?)
and case when factuurite0_.IsActive=1 then 'true' else 'false' end=case when ?='true' then 'true' else 'false' end
order by factuurite0_.Naam asc
哪个会导致异常:
{"Unable to cast object of type 'System.Boolean' to type 'System.String'."}
现在我的问题是:为什么?
Now for my question: why??
原始查询对我来说没问题.但是,SQL没有.这两个案例陈述从何而来?显然,它尝试将属性IsActive转换为SQL中的字符串,但这样做没有成功.
The original query looks ok to me. The SQL, however, does not. Where do the two case-statements originate from? Apparently it tries to convert the property IsActive to a string in SQL, which it fails to do.
编辑
好,找到解决方案.映射等没有问题,只是LINQ查询如何转换为SQL.特别是,该行的翻译方式:
Ok, found the solution. Nothing wrong with mapping etc., just with how the LINQ query is translated to SQL. In particular, how this line is translated:
&& item.IsActive == true
以某种方式,这被转换为复杂的CASE语句,最终导致异常消息.但是,== true
部分不是真正必要的.通过删除它,翻译器不再感到困惑,并提供了正确的SQL:
Somehow, this gets translated into the complex CASE-statement which ultimately results in the exception message. However, the == true
-part isn't really necessary. By removing it, the translator no longer gets confused and provides the proper SQL:
factuurite0_.IsActive=1
没有更多的案例说明,也没有更多的例外.
No more CASE-statement and no more exception.
推荐答案
好,找到了解决方案.映射等没有问题,只是LINQ查询如何转换为SQL.特别是,该行的翻译方式:
Ok, found the solution. Nothing wrong with mapping etc., just with how the LINQ query is translated to SQL. In particular, how this line is translated:
&& item.IsActive == true
以某种方式,这被转换为复杂的CASE语句,最终导致异常消息.但是,== true
部分不是真正必要的.通过删除它,翻译器不再感到困惑,并提供了正确的SQL:
Somehow, this gets translated into the complex CASE-statement which ultimately results in the exception message. However, the == true
-part isn't really necessary. By removing it, the translator no longer gets confused and provides the proper SQL:
factuurite0_.IsActive=1
没有更多的案例说明,也没有更多的例外.
No more CASE-statement and no more exception.
这篇关于nHibernate无法将布尔值转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!