Model view controller 紧急加载在EF中不工作

Model view controller 紧急加载在EF中不工作,model-view-controller,entity-framework-4,Model View Controller,Entity Framework 4,我正在拔头发,我不知道为什么我不能归还AdPhotos和Location实体。我使用的是.ToList(),它不应该保持AdPhotos集合的完整性吗 当我在返回上放置一个断点时,我可以在AdPhotos和Location中看到数据,但在那之后它就消失了 public List<AdListing> LatestAdListings() { using (var db = new AdultdirectoryEntities()) { var resu

我正在拔头发,我不知道为什么我不能归还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;
    }
}
public List LatestAdListings()
{
使用(var db=new AdultdirectoryEntities())
{
var结果=(来自db.AdListings.Include(“AdPhotos”).Include(“位置”)中的a)
在数据库中加入l。a.LocationID上的位置等于l.LocationID
其中a.Approved&&l.CountryID==Constants.ItemKeys.UsCountryId&&a.AdPhotos.Count>0
orderby a.CreateDateTime降序
选择a.Take(5.ToList();
返回结果;
}
}

您的
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();

在本页()中,我读到“当您调用Include时,查询路径仅对返回的ObjectQuery实例有效。ObjectQuery的其他实例和对象上下文本身不受影响。”我认为这是我的问题,是否有更好的解决方案返回完整关系?谢谢!这有帮助。我还有一个问题。如果我想对“位置”关系中的另一个关系执行此操作,该如何执行?