我有這個實體的<dynamic-component>
條目和一些 屬性。它被作爲 IDictionary
在實體類中消耗。NHibernate 3,動態組件,字典和LINQ查詢
映射工作正常,一切都是hunky dory直到 我去根據這個字典中的值進行查詢。
首先我嘗試以下LINQ查詢:
repository.Where(x => x.Specifications[key] == value)
要查詢反對。 (規格是動態組件)的 查詢導致以下錯誤:
Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'NHibernate.Type.ComponentType' to type 'NHibernate.Type.CollectionType'.
搞清楚這可能是LINQ提供程序然後我 繼續打造BaseHqlGeneratorForMethod
處理自定義LINQ 擴展的邊界之外爲了它。
var specificationExpression =
treeBuilder.Dot(
visitor.Visit(arguments[0]).AsExpression(),
treeBuilder.Ident("Specifications")).AsExpression();
var targetExpression =
treeBuilder.Dot(
visitor.Visit(arguments[0]).AsExpression(),
treeBuilder.Ident(keyExpression.Value.ToString())).AsExpression();
這生成正確的SQL除了 表達緩存,以便後續調用該函數將 比較所有值偉大的工作:
它是通過使用treeBuilder.Dot(...)
AST這樣建立起來的針對第一把鑰匙。
在這裏,我找到了treeBuilder.DictionaryItem(...)
AST節點和 建成了以下內容:
var specificationExpression =
treeBuilder.Dot(
visitor.Visit(arguments[0]).AsExpression(),
treeBuilder.Ident("Specifications")).AsExpression();
var specification =
treeBuilder.DictionaryItem(specificationExpression, key).AsExpression();
我再一次遭到了以下錯誤:
Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'NHibernate.Type.ComponentType' to type 'NHibernate.Type.CollectionType'.
問題
我在這裏做錯了什麼? <dynamic-component>
不能被查詢嗎?我是否錯誤地執行了這個?這可能是我應該報告的錯誤嗎?
映射:
<dynamic-component name="Specifications">
<property name="sp_Graphics" column="sp_Graphics" />
<property name="sp_Weight" column="sp_Weight" />
</dynamic-component>
實體:
/// <summary>
/// Specifications
/// </summary>
public virtual IDictionary Specifications { get; set; }
很好的捕獲。感謝您更新問題。 – Phill 2013-07-08 12:25:21