0
我想弄清楚如何使用.Include()從抽象類型中選擇包含實現類型的關係實體時,以下是我嘗試執行的一個示例:如何。抽象實現類型的.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
}