FluentNHibernate mapping for Dictionary

后端 未结 3 1697
深忆病人
深忆病人 2021-01-14 06:49

What is the best way of mapping a simple Dictionary property using Fluent NHibernate?

3条回答
  •  孤街浪徒
    2021-01-14 07:19

    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.

提交回复
热议问题