指定的包含路径无效.EntityType 未声明名称为 * 的导航属性 [英] A specified Include path is not valid. The EntityType does not declare a navigation property with the name *

查看:29
本文介绍了指定的包含路径无效.EntityType 未声明名称为 * 的导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从 LocalDb 获取到我的 MVC 控制器中.我试过这个:

I'm trying to get data from a LocalDb into my MVC Controller. I tried this:

UsersContext db = new UsersContext();
var users = db.UserProfiles.Where(u => u.UserId == WebSecurity.CurrentUserId)
                           .Include(u => u.LastName).ToList();

它返回这个错误:

指定的包含路径无效.实体类型ChatProj.Models.UserProfile"未声明导航属性名为姓氏".

A specified Include path is not valid. The EntityType 'ChatProj.Models.UserProfile' does not declare a navigation property with the name 'LastName'.

这是我的 localDb 和模型的图片.

知道为什么它不起作用吗?

Any idea why it's not working?

推荐答案

Navigation 属性应该是相关实体集合的实体类型.包含一些导航属性意味着加入您当前的实体与一些相关实体.这允许在单个查询中从多个表中预先加载数据.LastName 不是导航属性 - 它是简单的字段,默认情况下会加载,您不需要包含它:

Navigation property should be of entity type of collection of related entities. Including some navigation property means joining your current entity with some related entity or entities. That allows eager loading of data from several tables in single query. LastName is not a navigation property - it is simple field, and it will be loaded by default, you don't need to include it:

UsersContext db = new UsersContext();
var users = db.UserProfiles.Where(u => u.UserId == WebSecurity.CurrentUserId)
                           .ToList();

这个查询将被翻译成类似

This query will be translated into something like

SELECT UserId, UserName, LastName, FirstName 
FROM UserProfiles
WHERE UserId = @value

这篇关于指定的包含路径无效.EntityType 未声明名称为 * 的导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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