有一个简单的域..
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; }
}
有没有办法查询UserComment类型的所有实体并加载属性博客和照片?
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;
}
谢谢!