流畅的NHibernate和带索引器映射的类

时间:2010-12-10 09:10:00

标签: fluent-nhibernate nhibernate-mapping

如何使用流畅的映射

将类AttributeSet与Fluent NHibernate映射
public class AttributeSet : DictionaryBase
{
    private readonly Dictionary<string, object> _cache;

    public AttributeSet()
    {
        _cache = new Dictionary<string, object>();
    }

    public object this[string index]
    {
        get
        {
            return _cache[index];
        }
        set
        {
            _cache[index] = value;
        }
    }
}



public class Entity
{
    protected Entity()
    {
        Attributes = new AttributeSet();
    }

    public virtual int Id { get; set; }
    public virtual string Label { get; set; }
    public virtual string Description { get; set; }
    public virtual DateTime CreatedDate { get; set; }

    public virtual AttributeSet Attributes { get; set; }
}

1 个答案:

答案 0 :(得分:1)

我认为没有办法直接映射索引器,但您可以公开基础字典类型并映射它。

如果您不想将字典公开为公开字词,则可以将其映射为私有属性,而不是explained here。用于映射字典see here

一个例子可能是

HasMany<object>(Reveal.Member<AttributeSet>("Cache"))
    .KeyColumn("AttributeSetId")
    .AsMap<string>(idx => idx.Column("Index"), elem => elem.Column("Value"))
    .Access.CamelCaseField(Prefix.Underscore)

在字典中映射对象类型可能很有意思,我不知道NHibernate是否能够将它直接转换为底层数据库类型。