What is the best way of mapping a simple Dictionary property using Fluent NHibernate?
public class PersistedData
{
public virtual IDictionary Dictionary { get; set; }
}
public class PersistedDataMap : ClassMap
{
HasMany(x => x.Dictionary)
.Table("dict_table")
.KeyColumn("column_id")
.AsMap("key")
.Element("value");
}
This will properly map Dictionary
to table dict_table
and use column_id
to associate it to the base id.
As a side note, if you would like to use an Enum as the Key in the dictionary, it should be noted that NHibernate.Type.EnumStringType
can be used in place of the string in .AsMap
to use the string value instead of the Ordinal.