我试图弄清楚在从抽象类型中选择以包含已实现类型的关系实体时如何使用 .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
}