0

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

  }
4

1 回答 1

1

You can't do it this way. A different approach may work better for you:

Change UserProfile to this:

[Table("UserProfiles")]
public class UserProfile
{
    // ...
    public ICollection<PostComment> PostComments { get; set; }
    public ICollection<OtherComment> OtherComments { get; set; }
}

What EF does when you inherit a common type is create a discriminator column in a shared table. So when you select PostComments, the WHERE clause that EF generates will have something like AND type='PostComment'. (I don't remember the name of the column it generates, but you get the idea).

Then you can get the data like this:

var data = db.UserProfiles
                .Include("PostComments.Post")
                .Include("OtherComments.OtherData")
                .Single(p => p.UserId == WebSecurity.CurrentUserId);

If you want to use all the comments as a single list, you can create it like such:

var comments = data.PostComments.ToList<Comment>();
comments.AddRange(data.OtherComments);
于 2013-08-07T08:57:47.197 回答