开发者

NHibernate: mapping a complex value type with attributes?

I've been trying to map a IDictionary to a database using NHibernate (2.1.2.4000) and NHibernate.Mapping.Attributes (1.2.1.4000) with no success. I found a few blogs online that mention that it's possible to map to an attribute([1][2]), but I can't seem to get it to work because开发者_C百科 I keep getting the following error:

Error mapping generic collection FormsEntity.Attributes: expected 1 generic parameters, but the property type has 2

Attribute property looks like the following:

[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; }
}

Which in turn generates the following .hbm file:

<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>

Resources used:

  • [1] How to map to a Generic IDictionary
  • [2] NHibernate Mapping (the section of mapping a complex value type)

Other threads I've read

  • Map = IDictionary.
  • Which NHibernate.Mapping.Attributes should I use in my class to map a dictionary?


You need to specify how to map properties on your composite element:

    <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>

Substituting whatever your property names are.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜