nhibernate无法施放布尔人[英] nHibernate unable to cast Boolean to String

本文是小编为大家收集整理的关于nhibernate无法施放布尔人的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有以下查询:

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'."}

现在我的问题:为什么??

原始查询对我来说还不错.但是,SQL没有.两个案例陈述来自哪里?显然,它试图将属性iSActive转换为SQL中的字符串,但它无法做到.

编辑

好,找到了解决方案.映射等没有错,只是如何将LINQ查询转换为SQL.特别是如何翻译这条线:

&& item.IsActive == true

以某种方式,这将转化为复杂的病例陈述,最终导致异常消息.但是,== true - 部分并不是真正的必要条件.通过删除它,翻译器不再感到困惑,并提供了适当的SQL:

factuurite0_.IsActive=1

不再有案例陈述,也没有更多例外.

推荐答案

好的,找到了解决方案.映射等没有错,只是如何将LINQ查询转换为SQL.特别是如何翻译这条线:

&& item.IsActive == true

以某种方式,这将转化为复杂的病例陈述,最终导致异常消息.但是,== true - 部分并不是真正的必要条件.通过删除它,翻译器不再感到困惑,并提供了适当的SQL:

factuurite0_.IsActive=1

不再有案例陈述,也没有更多例外.

其他推荐答案

调试级别使用log4net?在某些版本的Hibernate和Log4net中,在调试级别上打开登录时,存在不相容性.您所得到的只是关于"无法执行SQL不能将布尔值施加到字符串"的错误.尝试将记录级别提高到信息,问题应该消失.

本文地址:https://www.itbaoku.cn/post/1556852.html

问题描述

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.

推荐答案

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.

其他推荐答案

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.