【问题标题】:Fluent NHibernate and class with indexer mappingFluent NHibernate 和带有索引器映射的类
【发布时间】:2010-12-10 09:09:59
【问题描述】:

如何使用流畅的映射将类 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; }
}

【问题讨论】:

    标签: fluent-nhibernate nhibernate-mapping


    【解决方案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 是否能够将其直接转换为底层数据库类型。

    【讨论】:

      猜你喜欢
      • 2011-02-05
      • 2011-04-10
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 2011-06-18
      • 1970-01-01
      相关资源
      最近更新 更多