nHibernate unable to cast Boolean to String

↘锁芯ラ 提交于 2019-12-24 01:42:32

问题


I have the following query:

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;

Which is converted to the following 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

Which results in the Exception:

{"Unable to cast object of type 'System.Boolean' to type 'System.String'."}

Now for my question: why??

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.

EDIT

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

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.


回答1:


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

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.




回答2:


Using Log4Net at the debug level? In some version of Hibernate and Log4Net there is an incompatibility when turn on logging at the DEBUG level. All you get is this error about 'unable to execute sql cannot cast boolean to string'. Try turning up your logging level to INFO and the problem should go away.



来源:https://stackoverflow.com/questions/6137888/nhibernate-unable-to-cast-boolean-to-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!