includes()函数不起作用

时间:2014-04-10 09:21:52

标签: c# entity-framework ef-code-first linq-to-entities

我正在使用EF 6和Code First,但出于某种原因,当我尝试从数据库中收集ICB列表时,依赖项(例如,站点)不会作为填充对象(icb.station)返回,只有站ID。我有这种关系。

公共类ICB     {

    public ICB()
    {
        ICBResources = new List<ICBResource>();
    }

    public int ICBId { get; set; }

    public int LevelId { get; set; }
    public virtual Station Station { get; set; }
    public string Location { get; set; }
    public DateTime TimeOfCall { get; set; }
    public DateTime TimeStarted { get; set; }

    public Staff IncidentCommander { get; set; }
    public Staff OperationsCommander { get; set; }
    public Staff SectorCommander { get; set; }
    public Staff SectorCommander2 { get; set; }
    public Staff CommandSupportOfficer { get; set; }
    public Staff SafetyOfficer { get; set; }
    public Staff CommunicationsOfficer { get; set; }
    public Staff WaterOfficer { get; set; }
    public Staff BAOfficer { get; set; }
    public Staff HazmatOfficer { get; set; }



    public virtual Company Company { get; set; }

}

我应该怎样做才能让所有这些成员都填满?他们只会提供ID。 我正在使用.Include()但不起作用。

    public List<ICB> GetAllICBs(Company company, Station station = null)
    {
        if (station != null)
        {
            return GetDbSet<ICB>()
                .Include("ICBResources")
                .Include("Station")
                .Where(i => i.Company.CompanyId == company.CompanyId 
                    && i.Station.StationId == station.StationId)
                .OrderByDescending(o => o.TimeStarted).ToList();
        }
        else
        {
            return GetDbSet<ICB>()
                .Include("ICBResources")
                .Include("Station")
                .Where(i => i.Company.CompanyId == company.CompanyId)
                .OrderByDescending(o => o.TimeStarted).ToList();
        }
    }

2 个答案:

答案 0 :(得分:0)

您的存储库中是否有GetDbSet方法?如果是,它可能返回没有相关表的数据,您应该使用早期存储库中的Include,即:

IQueryable icb = db.ICB.Include("ICBResources");

答案 1 :(得分:0)

包含方法的属性名称错误。

感谢您的帮助。