Reputation: 1426
I am new to using asp .net and LINQ queries. I wrote the following query but am getting an error.
Include path expression must refer to a navigation property defined on the type
In the above question there is the same error but they don't explain why it is happening at all. I believe it is because I included the inline .First() method on the third line below but again I want to know why this occurs and what it means. Thank you for your help.
Error:
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
Query:
IQueryable<User> users = db.Users
.Where(u => u.Email == email)
.Include(cu => cu.CompanyUsers.First())
.Include(c => c.Companies)
.Include(p => p.ParentCompanyAccounts );
Upvotes: 0
Views: 755
Reputation: 33381
You can user a transient property along with a persistent property.
class User
{
....
public virtual ICollection<User> CompanyUsers {get; set;} //persistent property
[NotMapped]
public User FirstCompanyUser //transient property
{
get{ return CompanyUsers.FirstOrDefault(); }
}
}
You can user a partial class to avoid code loss on regeneration if you use data first approach.
Upvotes: 0
Reputation: 16137
The problem lies in the 3rd line of your query. When you are including something using the Include
method, you can't just take one of an object. You have to take them all.
So where you have:
.Include(cu => cu.CompanyUsers.First())
Should be:
.Include(cu => cu.CompanyUsers);
For a good look at how to use Include
, I recommend taking a look at this MSDN post.
Upvotes: 1
Reputation: 203819
You cannot use First
in an Include
call. If you're going to use Include
, you need to include all of the related values.
Upvotes: 1