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

查看:423
本文介绍了指定的包含路径无效. 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();

它返回此错误:

指定的包含路径无效. EntityType 'ChatProj.Models.UserProfile'未声明导航属性 名称为"LastName".

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?

推荐答案

导航属性应为相关实体集合的实体类型.包括一些导航属性意味着将您的当前实体与某个或多个相关实体"加入".这样就可以在单个查询中渴望从多个表中加载数据. 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();

此查询将被翻译为类似

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

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

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