There is a simple domain..
public abstract class UserComment
{
public string Text { get; set; }
}
public class BlogComment : UserComment
{
public Blog Blog { get; set; }
}
public class PhotoComment : UserComment
{
public Photo Photo { get; set; }
}
Is there way to query all entities of type UserComment with properties Blog and Photo loaded?
var comment = DbContext.Set<UserComment>()
.Include(x => x.Blog) // will not compile
.Include(x => x.Photo) // will not compile
.FirstOrDefault();
if (comment is PhotoComment )
{
string url = (comment as PhotoComment).Photo.Url;
}
if (comment is BlogComment)
{
var dateCreated = (comment as BlogComment).Blog.DateCreated;
}
Thanks!
You will probably need two queries to get the result. If you only want the first element (
FirstOrDefault) explicit loading – as you already proposed in the comments – is a good approach:If you want to load a list of
UserComments that’s not the best solution as it would require to iterate over the loadedUserComments and to call explicit loading for each element which will result in many queries.For a list you can use the following approach that also will generate only two queries:
Because of the usage of
AsEnumerable()this will run two separate database queries and concat the results to a single collection in memory.LINQ-to-Entities supports
Castbut for some reason it is not possible to remove the twoAsEnumerable()conversions to get only a single database query and concat the results in the database. The code would still compile but I had a runtime exception about an invalidIncludepath.I have tested with EF 4.1. It might be worth to test the query without
AsEnumerable()with EF 5.0 to see if it still fails.