渴望加载不在EF工作

时间:2012-02-17 23:08:55

标签: model-view-controller entity-framework-4

我拔出我的头发,我不知道为什么我不能退回AdPhotos和Location实体。我正在使用.ToList()不应该保持AdPhotos集合完整吗?

当我在返回时放置一个断点时,我可以看到AdPhotos和Location中的数据,但之后它就消失了。

public List<AdListing> LatestAdListings()
{
    using (var db = new AdultdirectoryEntities())
    {
        var results = (from a in db.AdListings.Include("AdPhotos").Include("Location")
                       join l in db.Locations on a.LocationID equals l.LocationID
                       where a.Approved && l.CountryID == Constants.ItemKeys.UsCountryId && a.AdPhotos.Count > 0
                       orderby a.CreateDateTime descending
                       select a).Take(5).ToList();

        return results;
    }
}

1 个答案:

答案 0 :(得分:1)

您的Include电话之后是手动加入 - 不受支持。使用手动加入或投影后,您正在更改查询的形状,并且所有Include调用都将丢失。

此外,您不需要加入,因为您可以编写如下查询:

var results = (from a in db.AdListings.Include("AdPhotos").Include("Location")
               where a.Approved && a.Location.CountryID == Constants.ItemKeys.UsCountryId && a.AdPhotos.Count > 0
               orderby a.CreateDateTime descending
               select a).Take(5).ToList();
相关问题