在ravendb上使用延迟加载的linq查询中出现意外行为

时间:2011-08-19 19:46:34

标签: linq lazy-loading ravendb

我遇到以下代码的问题。如果我在初始ToList()调用时未调用RavenSession.Query<Item>(),则PhotoPath对象中的ItemSummaryModel属性为空。这是一个延迟加载问题还是导致此问题的其他问题?

此文档的初始保存时PhotoPath属性为null。然后我在随后的编辑中更新了它。

当我查询完整项目而不是选择新对象时,它按预期工作,填充所有属性。

为什么我必须使用ToList()强制执行查询,以便按预期填充新的ItemSummaryModel

var fullItems = RavenSession.Query<Item>().ToList();
var items = (from i in fullItems
             where i.DateAdded >= DateTime.Now.Subtract(new TimeSpan(10,0,0,0))
             orderby i.DateAdded
             select new ItemSummaryModel()
             {
                  Id = i.Id,
                  PhotoPath = i.ListingPhotoPath,
                  MarketingInfo = i.MarketingInfoShort,
                  Name = i.Name,
                  Summary = i.Summary,
                  PriceTypeCode = i.ClearancePrice > 0 ? PriceType.Clearance : (i.SalePrice > 0 ? PriceType.Sale : PriceType.List),
                  ListSaleOrClearancePrice = i.ClearancePrice > 0 ? i.ClearancePrice : (i.SalePrice > 0 ? i.SalePrice : i.Price)
             }).Take(nbrOfItems);
return items;

1 个答案:

答案 0 :(得分:1)

RavenDB的linq提供程序非常简单,它目前无法处理字段重映射。 换句话说,它执行此操作无法处理它:

                     PhotoPath = i.ListingPhotoPath,

如果您将其更改为

                     ListingPhotoPath = i.ListingPhotoPath,

它会起作用。 这是一个计划修复的问题