Entity Collection Property is Empty when Loading from RavenDB

被刻印的时光 ゝ 提交于 2019-12-10 15:25:15

问题


I started using RavenDB today. When I save a class, I can see the Collection property in the DB:

However, when I load the class, the collection has no items in it:

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

Is there some type of activation depth that needs to be set in order to see the properties that are POCOs?

Edit (to show the classes, by request):

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);
        }
    }
}

回答1:


Got it. There was no setter on the CustomVariables property. As soon as I added the private setter, it worked. So, apparently RavenDB doesn't use the private backing field, like db4o does. RavenDB needs the property.

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

    private set
    {
        this._customVariables = value;
    }
}



回答2:


Are you sure the query was executed? Try .ToArray() or .ToList():

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


来源:https://stackoverflow.com/questions/8661968/entity-collection-property-is-empty-when-loading-from-ravendb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!