Include路径表达式必须引用在type上定义的导航属性. [英] The Include path expression must refer to a navigation property defined on the type.in eager loading

查看:324
本文介绍了Include路径表达式必须引用在type上定义的导航属性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试包括这样的匿名类型: 除了CompanyTitlePeriodTypeName),我还希望所有incomelist属性

I try to include anonymous type like this : I want all incomelist attributes in addition to CompanyTitle ,PeriodTypeName )

 var incomeList = ctx.IncomeLists.Include(i => new
                {
                    CompanyTitle = i.CompanyId.ToString() + "/" + i.Company.CompanyName,
                    PeriodTypeName = i.ListPeriods.Select(lp => lp.PeriodType.PeriodTypeName)
                }).ToList()


我的模型部分如下:


My model section like this :

但是我得到以下异常:

包含路径表达式必须引用导航属性 在类型上定义.使用虚线路径进行参考导航 属性和用于集合导航的Select运算符 特性.参数名称:路径

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path

结果应该是Gridview的数据源.

推荐答案

您不能使用 Include 选择这样的数据. Include 用于加载相关数据.您应该使用 Include 加载实体,然后选择所需的实体.请记住从 CompanyId 中删除 .ToString(). EF会为您做到这一点.您的查询应如下所示:

You cannot use Include to select data like this. Include is used to load related data. You should load your entities using Include then select what you want. Remember to remove .ToString() from CompanyId. EF will do it for you. Your query should look like this:

var incomeList = ctx.IncomeLists
    .Include(i => i.Company)
    .Include(i => i.ListPeriods.Select(lp => lp.PeriodType))
    .Select(i => new 
    {
        CompanyTitle =  i.CompanyId + "/" + i.Company.CompanyName,
        PeriodTypeNames = i.ListPeriods.Select(lp => lp.PeriodType.PeriodTypeName)
    })
    .ToList();

这篇关于Include路径表达式必须引用在type上定义的导航属性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆