51

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

6 Answers 6

49

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
5
  • 1
    Cheers m8, great answer!
    – JazzMaster
    Commented Dec 17, 2013 at 15:24
  • 2
    @JazzMaster welcome :) consider also reading about Navigation Properties Commented Dec 17, 2013 at 15:26
  • @SergeyBerezovskiy, would you mind taking a look at my question? stackoverflow.com/questions/28988951/… Commented Mar 11, 2015 at 15:58
  • 1
    so there is no way to avoid always loading all properties in UserProfiles?
    – Toolkit
    Commented Jul 11, 2018 at 5:48
  • The most common mistake I see people, including myself, make, is Including an enum property. Or, refactoring an entity to just be an enum, and then this silently blows up in your face. Commented Aug 18, 2021 at 22:08
26

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

2
  • 1
    Couldn't've said it better, myself. Very succinct.
    – vapcguy
    Commented Jan 7, 2015 at 23:57
  • I didn't know where an error I had came from, untill I saw your answer. Said enough. +1
    – Max
    Commented May 28, 2018 at 8:09
22

Even though this is not quite related to the question, since Google brings you here, I thought it might be helpful to notice that a likely possibility is that you are using IEnumerable for your collection. Instead you should be using ICollection, see more here: https://stackoverflow.com/a/32997694/550975

This seems to be a problem in EF6 and perhaps earlier versions only... no longer a problem to use either in EF Core.

0
8

If you want to retrieve only the LastName, use

Select(m => m.LastName)

so

 var users = db.UserProfiles
                .Where(u => u.UserId == WebSecurity.CurrentUserId)
                .Select(u => u.LastName)//not Include
                .ToList();

LastName is just a string (Scalar property) in your model, not a Navigation property (relation with another entity)

0

As this is the top answer on google for this and to help others, this can also be the error with a misconfigured navigation property. Ensure the virtual property has both a getter and setter or it will fail.

public class UserSibling
{
    public int UserId { get; set; }
        
    [ForeignKey(nameof(UserId))]
    [CanBeNull]
    public virtual User User { get; set; }
}
-2

In my case I solved it as the following

the code with the error :

LabResults = db.LAB_RESULTS.Where(o => o.ORDER_ID == id)
.Include(p => p.LabTests).ToList()

then I removed .Include :

LabResults = db.LAB_RESULTS.Where(o => o.ORDER_ID == id).ToList()
1
  • This solution is not directly relevant to question and doesn't solve it either. :) Commented Nov 13, 2022 at 23:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.