简体   繁体   English

NHibernate映射IDictionary问题

[英]NHibernate mapping an IDictionary problems

I have an NHibernate mapping issue which I could use help with - the documentation & samples seem very unclear to me. 我有一个NHibernate映射问题,可以向您提供帮助-文档和示例对我来说还不清楚。

I have one class which contains a dictionary of another classs. 我有一个班级,其中包含另一个班级的字典。 I'm unsure of the correct mapping and everything I've tried either fails to map, or gets a NHibernate exception on reading data. 我不确定正确的映射,我尝试过的一切都无法映射,或者在读取数据时出现NHibernate异常。

The 2 classes are: 这两个类是:

public class SnippetConfigParam
{
    public virtual int          SnippetValueKey { get; set; }
    public virtual string       ParamName { get; set; }
    public virtual string       Value{ get; set; }
}

public  class SnippetConfig
{
    public virtual int          SnippetKey { get; set; }


    public virtual ESnippetType Type{ get; set; }
    public virtual string       Name { get; set; }

    public virtual IDictionary<string, SnippetConfigParam> Params { get; set; }


    public virtual string       Description{ get; set; }


    public virtual VendorPage   Page{ get; set; }
}

My last mapping attempt was 我最后一次映射尝试是

<map name="Params"  table="SnippetConfigValue" lazy="false">
    <key column="SnippetConfigKey" />
    <index column="SnippetValueKey"  type="Int32"/>    
    <composite-element class ="SnippetConfigParam">
        <property name="ParamName"  />
        <property name="Value" />
    </composite-element>      
</map>

Which results in: 结果是:

System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'. System.InvalidCastException:无法将类型为System.Int32的对象转换为类型为System.String的对象。

So I'm clearly not understanding something. 因此,我显然不了解某些内容。 The database tables are: 数据库表是:

Create Table SnippetConfig
    SnippetKey  int not null, 
    ...
    PrimaryKey( 'SnippetKey' )

  Create Table SnippetConfigValue
    SnippetValueKey  int  not null,
    ...
    PrimaryKey( 'SnippetValueKey' ),
    Key  'fk' ( 'SnippetConfigKey' ),
    Constraint 'fk' foreign key ('SnippetConfigKey' ) references 'SnippetConfig' ('SnippetKey' )...

Any advice would be greatly appreciated. 任何建议将不胜感激。

Mapping dictionary in NHibernate could be a bit tricky. NHibernate中的映射字典可能有些棘手。 See the detail description here: Ayende's post about <map> . 在此处查看详细说明: Ayende关于<map>的帖子 The key to understand it is in the mapping of the TKey and TValue ( IDictionary<TKey, TValue> ) 理解它的关键在于TKeyTValue的映射( IDictionary<TKey, TValue>

  • TKey is represented by <index> element TKey由<index>元素表示
  • TValue could be element, composite-element, one-to-many TValue可以是元素,复合元素,一对多

Because your TKey is string : 因为您的TKey字符串

public virtual IDictionary<string, SnippetConfigParam> Params { get; set; }

The mapping representing it cannot be the int 表示它的映射不能是int

<index column="SnippetValueKey"  type="Int32"/>

Change the Params proeprty to be IDictionary<int, SnippetConfigParam> and it should work. 参数属性更改为IDictionary<int, SnippetConfigParam> ,它应该可以工作。

NOTE: If you want the property SnippetValueKey of the class SnippetConfigParam to be filled extend the component 注意:如果要填充SnippetConfigParam类的SnippetValueKey属性,请扩展该组件

<composite-element class ="SnippetConfigParam">
    <property name="SnippetValueKey" formula="SnippetValueKey " 
              insert="false" update="false" />
    <property name="ParamName"  />
    <property name="Value" />
</composite-element>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM