抽象型から選択して実装型の関係エンティティを含めるときに .Include() を使用する方法を理解しようとしています。これは、私がやろうとしていることの例です:
[Table("Comments")]
public abstract class Comment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CommentId { get; set; }
public int UserId { get; set; }
public virtual UserProfile User { get; set; }
public string Content { get; set; }
}
[Table("PostComments")]
public class PostComment : Comment
{
public int PostId { get; set; }
public virtual Post Post { get; set; }
}
[Table("UserProfiles")]
public class UserProfile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[MaxLength(200)]
public string UserName { get; set; }
public ICollection<Comments> Comments { get; set; }
}
using (DataContext db = new DataContext())
{
return db.UserProfiles.Include(x => x.Comments)
.Single(u => u.UserId == WebSecurity.CurrentUserId);
// Here I need a way to include Comment.Post if Comment
// is PostComment or some other child entity if Comment is
// from another inherited type
}