NHibernate:使用属性映射复杂值类型?

时间:2011-03-25 08:04:33

标签: nhibernate nhibernate-mapping asp.net-4.0

我一直在尝试使用NHibernate(2.1.2.4000)和NHibernate.Mapping.Attributes(1.2.1.4000)将IDictionary映射到数据库但没有成功。我在网上发现了一些博客,提到可以映射到一个属性([1] [2]),但我似乎无法让它工作,因为我不断收到以下错误:

  

映射泛型集合的错误FormsEntity.Attributes:期望1个泛型参数,但属性类型有2个

属性属性如下所示:

[Map(2, Name = "Attributes", Cascade = CascadeStyle.All)]
[Key(3, Column = "FormsEntityID")]
[Index(4, Column = "Name", Type = "string")]
[CompositeElement(5, ClassType=typeof(HtmlAttribute))]
public virtual IDictionary<string, HtmlAttribute> Attributes
{
    get { return _attributes; }
    set { _attributes = value; }
}

然后生成以下.hbm文件:

<hibernate-mapping auto-import="false" xmlns="urn:nhibernate-mapping-2.2">
    <joined-subclass name="FormsEntity, Entities" extends="BaseEntity, Entities" 
        table="CMS_FormsEntity ">

        <key column="Id" />
        <property name="Title" />
        <property name="Description">
            <column name="description" sql-type="nvarchar(MAX)" />
        </property>
        <property name="IsTemplate" />

        <map name="Attributes" cascade="all">
              <key column="FormsEntityID" />
              <index column="Name" type="string" />
              <composite-element class="HtmlAttribute, Entities" />
        </map>

    </joined-subclass>
</hibernate-mapping>

使用的资源:

我读过的其他帖子

1 个答案:

答案 0 :(得分:0)

您需要指定如何在复合元素上映射属性:

    <map name="Attributes" cascade="all">
          <key column="FormsEntityID" />
          <index column="Name" type="string" />
          <composite-element class="HtmlAttribute, Entities">
               <property name="Name" />
               <property name="Value" />
          </composite-element>
    </map>

用你的财产名称代替。