2011-05-26 54 views
2

我有以下查詢:NHibernate的無法施展布爾到字符串

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中的字符串,但它無法執行。

EDIT

好的,找到了解決辦法。映射等沒有錯,只是LINQ查詢被轉換爲SQL。特別是,如何這條線被翻譯:

&& item.IsActive == true 

不知何故,這個被翻譯成複雜的CASE語句最終導致異常消息。但是,== true-部分不是真的必要。通過刪除它,翻譯器不再會感到困惑並提供正確的SQL:

factuurite0_.IsActive=1 

不再有CASE語句,也沒有更多的異常。

+1

請向我們展示FactuurItem的映射。該映射定義了每個字段將映射到哪個SQL類型。 – 2011-05-26 12:50:49

+1

既然您找到了解決方案,您可以通過在下面的框中提供答案來回答自己的問題。接下來,您可以通過單擊刻度圖標(但不是馬上)接受它。 – 2011-05-26 13:50:13

回答

2

好的,找到了解決方案。映射等沒有錯,只是LINQ查詢被轉換爲SQL。特別是,如何這條線被翻譯:

&& item.IsActive == true 

不知何故,這個被翻譯成複雜的CASE語句最終導致異常消息。但是,== true-部分不是真的必要。通過消除它,譯者不再迷糊和提供適當的SQL:

factuurite0_.IsActive=1 

沒有更多的CASE語句並沒有更多的例外。

0

在調試級別使用Log4Net?在某些版本的Hibernate和Log4Net中,在DEBUG級別打開日誌時存在不兼容性。你所得到的是'無法執行sql無法將布爾轉換爲字符串'的錯誤。嘗試將日誌記錄級別設置爲INFO,問題應該消失。