实体框架显式加载多级

6

我尝试使用显式加载来加载多级数据,但是出现了错误:

"属性路径 'Channel.Posts' 不能用于导航属性。属性路径只能用于访问基元或复杂属性。"

这是我的代码:

var listSubs;

using (var db = new GamePortalContext())
{
    listSubs = db.Subscribers.Include("Channel").Where(o => o.User.Username == username.ToLower() &&  o.Channel.IsActive && o.Channel.IsPublic && o.Channel.Posts.Count(p => p.PublishTime <= DateTime.Now && p.IsActive && p.IsHot) > 0);
    if (listSubs.Any())
    {
        listSubs = listSubs.OrderByDescending(o => o.Channel.ChannelTrack.LastPublishTime);
        listSubs = (num == int.MinValue) ? listSubs : listSubs.Take(num);
        foreach (var item in listSubs)
        {
            db.Entry(item).Collection(o => o.Channel.Posts).Query().Where(i => i.IsHot && i.IsActive && i.PublishTime <= DateTime.Now).Take(numpost).Load();
        }

        return listSubs.ToList();
    }
    else
    {
        return null;
    }
}

这是我的帖子和频道实体

public partial class Post

{

  public Post()

  {

       this.ReadPostLaters = new HashSet<ReadPostLater>();

   }
    public string PostId { get; set; }
    public string Name { get; set; }
    public string Alias { get; set; }
    public string Description { get; set; }
    public bool IsActive { get; set; }
    public bool IsHot { get; set; }
    public System.DateTime CreatedAt { get; set; }
    public System.DateTime PublishTime { get; set; }
    public int Views { get; set; }
    public bool IsSticked { get; set; }
    public int UpdatedTime { get; set; }
    public bool IsSaved { get; set; }
    public Nullable<int> ChannelId { get; set; }
    public long UserId { get; set; }
    public int PostType { get; set; }
    public string UrlAvatar { get; set; }
    public virtual Article Article { get; set; }
    public virtual Channel Channel { get; set; }
    public virtual Event Event { get; set; }
    public virtual User User { get; set; }
    public virtual ICollection<ReadPostLater> ReadPostLaters { get; set; }
    public virtual Video Video { get; set; }
}

public partial class Channel

{

   public Channel()
   {
        this.Ads = new HashSet<Ad>();
        this.ChannelAdmins = new HashSet<ChannelAdmin>();
        this.ChannelPlayers = new HashSet<ChannelPlayer>();
        this.Notifications = new HashSet<Notification>();
        this.Posts = new HashSet<Post>();
        this.Subscribers = new HashSet<Subscriber>();
    }
    public int ChannelId { get; set; }
    public string Name { get; set; }
    public string Username { get; set; }
    public int Voters { get; set; }
    public int Subs { get; set; }
    public float SiteScore { get; set; }
    public float UserScore { get; set; }
    public string HomeUrl { get; set; }
    public string FanpageUrl { get; set; }
    public string Publisher { get; set; }
    public int Players { get; set; }
    public Nullable<System.DateTime> PublishDate { get; set; }
    public string Status { get; set; }
    public bool IsActive { get; set; }
    public bool IsHot { get; set; }
    public bool IsPublic { get; set; }
    public bool IsNew { get; set; }
    public bool IsChanneling { get; set; }
    public int CategoryId { get; set; }
    public string UrlAvatar { get; set; }
    public string UrlCover { get; set; }
    public virtual ICollection<Ad> Ads { get; set; }
    public virtual CategoryChannel CategoryChannel { get; set; }
    public virtual ICollection<ChannelAdmin> ChannelAdmins { get; set; }
    public virtual ICollection<ChannelPlayer> ChannelPlayers { get; set; }
    public virtual ChannelTrack ChannelTrack { get; set; }
    public virtual ICollection<Notification> Notifications { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
    public virtual ICollection<Subscriber> Subscribers { get; set; }
}

请发布您的Channel类,或者无论Channel是什么类型。 - Steve
一个频道包括帖子,频道是从我的数据库生成的实体,并与帖子实体相关联:一对多。 - Thanh Son Nguyen
你还应该发布你的“帖子”实体。 - Steve
我刚刚在我的问题中发布了Channel和Post实体。 - Thanh Son Nguyen
2个回答

7

修改

db.Entry(item).Collection(o => o.Channel.Posts).Query().Where(i => i.IsHot && i.IsActive && i.PublishTime <= DateTime.Now).Take(numpost).Load();

to

db.Entry(item.Channel).Collection(o => o.Posts).Query().Where(i => i.IsHot && i.IsActive && i.PublishTime <= DateTime.Now).Take(numpost).Load();

1
尝试更改此行:

listSubs = db.Subscribers.Include("Channel").Where(o => o.User.Username == username.ToLower() &&  o.Channel.IsActive && o.Channel.IsPublic && o.Channel.Posts.Count(p => p.PublishTime <= DateTime.Now && p.IsActive && p.IsHot) > 0);

同时调用 .Include("Channel.Posts"):

listSubs = db.Subscribers.Include("Channel").Include("Channel.Posts") .. etc;

1
我想对帖子应用过滤器。EagerLoading(您的演示)无法应用过滤器。我该怎么办? - Thanh Son Nguyen
@Steve 谢谢你分享.Include技巧...我一直在想如何从我的数据库中显式加载链接级联表的数据。这种方法非常简单快捷,只触发一个SQL请求!! - Ephie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接