从RavenDB加载时,实体集合属性为空

时间:2011-12-28 22:23:08

标签: c# ravendb

我今天开始使用RavenDB。当我保存一个类时,我可以在DB中看到Collection属性:

enter image description here

但是,当我加载该类时,该集合中没有任何项目:

public IEnumerable<CustomVariableGroup> GetAll()
{
    using (IDocumentSession session = Database.OpenSession())
    {
        IEnumerable<CustomVariableGroup> groups = session.Query<CustomVariableGroup>();
        return groups;
    }
}

enter image description here

是否需要设置某种类型的激活深度才能查看属于POCO的属性?

编辑(按要求显示课程):

public class EntityBase : NotifyPropertyChangedBase
{
  public string Id { get; set; }  // Required field for all objects with RavenDB.
}

    public class CustomVariableGroup : EntityBase
{
    private ObservableCollection<CustomVariable> _customVariables;       

    public ObservableCollection<CustomVariable> CustomVariables
    {
        get
        {
            if (this._customVariables == null)
            {
                this._customVariables = new ObservableCollection<CustomVariable>();
            }
            return this._customVariables;
        }
    }
}

    public class CustomVariable : EntityBase
{
    private string _key;
    private string _value;

    /// <summary>
    /// Gets or sets the key.
    /// </summary>
    /// <value>
    /// The key.
    /// </value>
    public string Key
    {
        get { return this._key; }

        set
        {
            this._key = value;
            NotifyPropertyChanged(() => this.Key);
        }
    }

    /// <summary>
    /// Gets or sets the value.
    /// </summary>
    /// <value>
    /// The value.
    /// </value>
    public string Value
    {
        get { return this._value; }

        set
        {
            this._value = value;
            NotifyPropertyChanged(() => this.Value);
        }
    }
}

2 个答案:

答案 0 :(得分:3)

知道了。 CustomVariables属性上没有setter。一旦我添加了私人二传手,它就有效了。所以,显然RavenDB不像db4o那样使用私有支持字段。 RavenDB需要该属性。

public ObservableCollection<CustomVariable> CustomVariables
{
    get
    {
        if (this._customVariables == null)
        {
            this._customVariables = new ObservableCollection<CustomVariable>();
        }
        return this._customVariables;
    }

    private set
    {
        this._customVariables = value;
    }
}

答案 1 :(得分:1)

您确定查询已执行吗?试试.ToArray().ToList()

public IEnumerable<CustomVariableGroup> GetAll()
{
    using (IDocumentSession session = Database.OpenSession())
    {
        IEnumerable<CustomVariableGroup> groups = session.Query<CustomVariableGroup>();
        return groups.ToList();
    }
}