Include Related Derived Models

Here's the Lambda expression that I use to try to include a User table that causes 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 specify a navigation property defined on the type. Use the dashed paths for reference navigational properties and the Select operator for navigational properties of the collection.

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 to include a User table for a ProjectDoc type Cover ?

UPDATE : For the answer. I updated the model for Cover to look like this and removed the include, which, as I said, caused an error. Now I can get the data:

 public class Cover : ProjectDoc { public string CoverURL { get; set; } public int UserID { get; set; } public virtual User User { get; set; } } 
+7
source share
1 answer

Not currently supported . Unwanted load relationships by derived types do not work. The best thing you can do is to execute a separate request to download all the necessary Users for Covers already loaded with the first request, and let EF do its magic (it should fill your navigation properties on already loaded objects, but you must disable lazy loading).

+2
source

All Articles