6

I tried load multi-level with explicit loading but an error occurred:

"The property path 'Channel.Posts' cannot be used for navigation properties. Property paths can only be used to access primitive or complex properties."

This is my code:

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;
    }
}

Here is my post and channel entity

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; }
}
4
  • Post your Channel class please, or whatever type Channel is. Commented Jul 27, 2013 at 5:59
  • a Channel include posts, Channel is an Entity generate from my DB, and Relate with Posts Entity: one-many. Commented Jul 27, 2013 at 6:08
  • You should also post your Post entity. Commented Jul 27, 2013 at 6:27
  • i have just posted Channel and Post entity in my question Commented Jul 27, 2013 at 6:48

2 Answers 2

7

change

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();
Sign up to request clarification or add additional context in comments.

Comments

1

Try changing this line:

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);

To also call .Include("Channel.Posts"):

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

2 Comments

I want to apply filter to posts.EagerLoading (your demo) can't not apply filter. what can i do?
@Steve thanks for the .Include technique... I was wondering how I could explicit load data from linked cascading tables in my database. This approach is really simple and fast and triggers only one sql request!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.