c# - includes() 函数不起作用

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

我使用的是 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/

相关文章:

c# - 带有 “Yes” 的消息框,C# 中的 “No” 选择?

C# 如何从程序发送 ENTER?

c# - ASP.Net MVC 用户列表及其角色

c# - 插入数据库时​​出错 - 值不能为空

c# - 在 C# 中,将 List<T> 转换为 SortedDictionary<string, T> 的最佳方法是什么?

c# - Mono 编译器即服务 (MCS)

entity-framework - 不确定如何映射多对多关系 - 重用不同实体的映射表

entity-framework-4.1 - Entity Framework 4.1代码优先的命令超时

c# - Entity Framework Include() 返回 null 导航属性

entity-framework - EF 4.1 升级后的代码第一期