代码之家  ›  专栏  ›  技术社区  ›  Chandam

字典的FluentHibernate映射

  •  3
  • Chandam  · 技术社区  · 15 年前

    使用fluent nhibernate映射简单字典属性的最佳方法是什么?

    3 回复  |  直到 8 年前
        1
  •  3
  •   Kevin Panko Matthew Woodard    8 年前

    要将列表映射为字典:

    HasMany(x => x.Customers)
      .AsMap();
    

    我没有用过,所以不能举个例子。

    看看wiki: Cached version of the page , Actual page 我已经给出了页面的缓存版本,因为该站点似乎已关闭。

        2
  •  6
  •   Kevin Panko Matthew Woodard    8 年前
    public class PersistedData 
    {
        public virtual IDictionary<key, value> Dictionary { get; set; }
    }
    
    public class PersistedDataMap : ClassMap<PersistedData>
    {
        HasMany(x => x.Dictionary)
                .Table("dict_table")
                .KeyColumn("column_id")
                .AsMap<string>("key")
                .Element("value");
    }
    

    这将正确映射 Dictionary 到桌子 dict_table 和使用 column_id 将其与基ID关联。

    作为附加说明,如果要使用枚举作为字典中的键,则应注意 NHibernate.Type.EnumStringType<MyEnum> 可用于代替字符串 .AsMap<string> 使用字符串值而不是序号。

        3
  •  6
  •   Kevin Panko Matthew Woodard    8 年前

    使用简单的类关系,如:

    public class Foo {
        public virtual IDictionary<string, Bar> Bars { get; set; }
    }
    
    public class Bar {
        public virtual string Type { get; set; }
        public virtual int Value { get; set; }
    }
    

    你可以用流畅的NHibernate绘制这个图:

    mapping.HasMany(x => x.Bars)
           .AsMap(x => x.Type);
    

    在哪里? Bar.Type 用作字典中的关键字字段。