A specified Include path is not valid. The EntityType does not declare a navigation property with the name *

c# entity-framework linq

Question

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();

It returns this error:

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

Here is a picture of my localDb and model.

Any idea why it's not working?

1
40
12/17/2013 3:26:53 PM

Accepted Answer

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
39
12/17/2013 3:21:39 PM

Popular Answer

Include is only for navigation properties, and LastName is a scalar property, so you don't need Include at all.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow