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

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

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

是否需要设置某种类型的激活深度才能查看 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);
        }
    }
}

最佳答案

知道了。 CustomVariables 属性上没有 setter 。一旦我添加了私有(private)二传手,它就起作用了。因此,显然 RavenDB 不像 db4o 那样使用私有(private)支持字段。 RavenDB 需要该属性。

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

    private set
    {
        this._customVariables = value;
    }
}

关于c# - 从 RavenDB 加载时实体集合属性为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8661968/

10-13 07:35
查看更多