我使用的是 EF 6 和 Code First,但出于某种原因,当我尝试从数据库中收集 ICB 列表时,相关性(例如 Station)不会作为填充对象(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();
}
}
最佳答案
您好,您的存储库中有 GetDbSet 方法吗?如果是,它可能会返回没有相关表的数据,您应该在存储库中更早地使用 Include,即:
IQueryable icb = db.ICB.Include("ICBResources");
关于c# - includes() 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22983854/