问题
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