我遇到了一些問題,已添加到更新版本的Castle的ActiveRecord中的SqlNamedQuery屬性。我已指定這樣的:Castle ActiveRecord:'System.Int32'無法轉換爲類型'System.Boolean'
[assembly: SqlNamedQuery(Queries.GetItemName, "EXEC [dbo].[GetItemName] :id")]
除此之外,我對結果映射屬性聲明:
...
[Property(Access = PropertyAccess.AutomaticProperty, NotNull = true)]
public virtual bool IsPrimaryName { get; set; }
...
NHibernate的查詢:
IQuery query = Session.GetNamedQuery(Queries.GetItemName);
query.SetParameter("id", 1212, NHibernateUtil.Int64);
query.SetResultTransformer(Transformers.AliasToBean<Mapping>());
執行後,我收到了以下例外:
Object of type 'System.Int32' cannot be converted to type 'System.Boolean'.
我還添加了下面的web.config條目但沒有成功(可能只適用於hql查詢)。
<add key="query.substitutions" value="true 1, false 0, yes 'Y', no 'N'" />
所以,我的問題是 - 我該如何解決這個問題?存儲過程返回0或1,但我也嘗試了'1','true','TRUE'等。我怎樣才能解決這個問題?或者,也許我應該刪除SqlNamedQueries?
UPDATE:堆棧跟蹤
[ArgumentException: Object of type 'System.Int32' cannot be converted to type 'System.Boolean'.]
System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast) +4070954
System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr) +9631414
System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig) +151
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +223
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +28
System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) +101
System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index) +25
NHibernate.Properties.BasicSetter.Set(Object target, Object value) +68
UPDATE:Select語句:
SELECT
itm.ItemId AS ItemId,
itm.Value AS Value,
itm.Id AS ParentId,
1 AS IsPrimaryName
FROM
[dbo].[Item] AS itm
...
正如我上面提到我曾嘗試對SP側多種不同的方案,其中包括:1, 'true','TRUE'+聲明一個BIT變量,並直接從select語句返回它,但它都會失敗,並出現類似的異常。
Thx!
您使用哪種方言和哪種版本的NHibernate? – jishi