我的一个类中有一个XPCollection。它是另一个类的XPCollection。当我运行应用程序时,XPCollection不会被加载。我可以在调试时在断点中清楚地看到它。这是我在我的对象中使用的代码。 :
private XPCollection<LimitAllocationTotals> _LimitAllocationTotals;
public XPCollection<LimitAllocationTotals> LimitAllocationTotals LimitAllocationTotals
{
get
{
if (LimitAllocations.IsLoaded && LimitAllocations != null)
{
UnitOfWork uow = new UnitOfWork();
_LimitAllocationTotals = new XPCollection<LimitAllocationTotals>(uow, new BinaryOperator("Oid", Guid.Empty));
_LimitAllocationTotals.Session = this.Session;
foreach (LimitAllocation allocation in LimitAllocations)
{
LimitAllocationTotals allocationTotals = new LimitAllocationTotals(this.Session);
allocationTotals.MTMLimit += allocation.MTMLimit;
allocationTotals.ARLimit += allocation.ARLimit;
allocationTotals.Volume += allocation.Volume;
allocationTotals.MaxTenor = allocation.MaxTenor;
allocationTotals.SourceEntity = allocation.SourceEntity;
allocationTotals.Commodity = allocation.Commodity;
allocationTotals.ConvertedVolume = allocation.ConvertedVolume;
_LimitAllocationTotals.Add(allocationTotals);
}
foreach (LimitExtension extension in LimitExtensions)
{
if (extension.ExpirationDate >= DateTime.Now)
{
LimitAllocationTotals searchAllocation = null;
foreach (LimitAllocationTotals allocation in _LimitAllocationTotals)
{
if (allocation.SourceEntity != null && allocation.SourceEntity.Oid.Equals(extension.SourceEntity.Oid))
{
searchAllocation = allocation;
break;
}
}
if (searchAllocation == null)
{
searchAllocation = new LimitAllocationTotals(this.Session);
searchAllocation.SourceEntity = extension.SourceEntity;
_LimitAllocationTotals.Add(searchAllocation);
}
searchAllocation.MTMLimit += extension.MTMLimit;
searchAllocation.ARLimit += extension.ARLimit;
searchAllocation.Volume += extension.Volume;
searchAllocation.Commodity = extension.Commodity;
searchAllocation.MaxTenor += extension.MaxTenor;
// searchAllocation.CalculateMeasureConversion(_Commodity, _VolumeUnit);
//searchAllocation.VolumeUnit = extension.VolumeUnit;
}
}
}
return _LimitAllocationTotals;
}
set
{
SetPropertyValue("LimitAllocationTotals", ref _LimitAllocationTotals, value);
}
}
答案 0 :(得分:0)
按需加载XPCollection
。来自the documentation:
在大多数情况下,需要创建一个集合来访问一个对象 数据存储中的特定类型。
XPCollection
类实现 延迟加载,即它填充了来自的持久对象 按需存储数据。例如,您可以创建XPCollection
实例,标记为包含自定义Person对象。该 但是,集合将使用数据中的Person对象填充 仅在访问集合的内容时存储。
您可以通过调用XPCollection.Load()
强制来自数据存储的加载,但通常是this should not be necessary。
答案 1 :(得分:0)
您可以在OnLoaded方法上安全地初始化该集合,或者您可以在属性getter中检查IsLoading和IsSaving属性值,如果其中任何一个为true,则返回。只有当(IsLoading || IsSaving)== false时,您才能安全地从DB加载集合。
你可以尝试使用getter方法;
public XPCollection<LimitAllocationTotals> LimitAllocationTotals
{
get
{
if (IsLoading || IsSaving)
return _LimitAllocationTotals;
// .... your codes
}
}