Here's the Lambda expression I am using to try and include the User
table, which throws an error.
ICollection<Activity> activity = db.Activities
.Include(i => i.Project.ProjectDoc.OfType<Cover>().Select(v => v.User))
.Where(u => u.UserID == WebSecurity.CurrentUserId)
.OrderByDescending(d => d.DateCreated).ToList();
The include statement gives this 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.
The model in question
public abstract class ProjectDoc
{
public int ProjectDocID { get; set; }
public int ProjectID { get; set; }
public string DocTitle { get; set; }
public string Status { get; set; }
public string Access { get; set; }
public DateTime DateCreated { get; set; }
public virtual ProjectDocAccess ProjectDocAccess { get; set; }
public virtual Project Project { get; set; }
public virtual ICollection<Comment> Comment { get; set; }
public ICollection<ProjectDocVote> ProjectDocVote { get; set; }
}
public class Segment : ProjectDoc
{
public string Content { get; set; }
}
public class Cover : ProjectDoc
{
public string CoverURL { get; set; }
public int UserID { get; set; }
public User User { get; set; }
}
How do I include the User
table for the ProjectDoc
of type Cover
?
UPDATE:
Per the answer. I updated the model for Cover
to looks like this and removed the include that I said was causing the error. I can now get the data:
public class Cover : ProjectDoc
{
public string CoverURL { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
}
It is currently not supported. Eager loading relations on derived types doesn't work. The best you can do is to execute separate query to load all required Users for Covers already loaded by the first query and let EF do its magic (it should fill your navigation properties on already loaded entities but you must have lazy loading turned off).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With