NHibernate 3.2 By Code (Conformist) ClassMapping For a Dictionary Property

限于喜欢 提交于 2020-01-14 14:14:12

问题


Assume I have a class "SomeClass" that has a lookup dictionary: DataDictionary;

I currently have a mapping in SomeClass.hbm.xml like this:

<class name="SomeClass>

  <id name="ID" type="System.Guid">
    <generator class="guid" />
  </id>

  <map name="DictionaryProperty" table="SomeClass_Data">
    <key column="SomeClassID" />
    <index column="Key" type="System.String" />
    <element column="Value" type="System.String" />
  </map>

</class>

I want to use NHibernate's new (version 3.2) By Code mappings. How would I map the dictionary property above?

Currently I have:

  public class SomeClassMap :ClassMap<SomeClass>
  {

     public SomeClassMap()
     {
        Id(x => x.ID, mapping => mapping.Generator(Generators.Guid));
        Map(x = x.DictionaryProperty, mapping =>
           {
              mapping.Key(k => k.Column("SomeClassID"));
              mapping.Table("SomeClassData");
           });
     }

  }

Mostly I am at a loss for how to specify the equivalent of the index and the element for a dictionary mapping.


回答1:


Each part of the dictionary mapping requires a separate delegate:

Map(x => x.DictionaryProperty,
    mapping =>
    {
        mapping.Key(k => k.Column("SomeClassID"));
        mapping.Table("SomeClassData");
    },
    mapping => mapping.Element(k => k.Column("Key")),
    mapping => mapping.Element(k => k.Column("Value")));

The first Element is the map-key (index is obsolete for map), and the second one is the element.



来源:https://stackoverflow.com/questions/8345672/nhibernate-3-2-by-code-conformist-classmapping-for-a-dictionary-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!