2

Good morning everyone,

I am trying to tackle a problem I run into with EF code first. My schema is the following

   public class Article : IUrlNode 
{
    [Key]
    public Guid ArticleID { get; set; }
    public string Title { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateUpdated { get; set; }
    public string Summary { get; set; }
    [System.ComponentModel.DataAnnotations.InverseProperty("CategoryArticles")]
    public virtual IQueryable<Category> ArticleCategories { get; set; }

    public string FriendlyUrl
    {
        get;
        set;
    }
}
   [RouteChild("CategoryArticles")]
   public class Category : ContentNode
{
    public Guid ServiceId { get; set; }

    [System.ComponentModel.DataAnnotations.InverseProperty("ArticleCategories")]
    public virtual IQueryable<Article> CategoryArticles { get; set; }
}

I have written code with which I am able to retrieve a category from the database without actually knowing that its a category. From then on I must retrieve a single article of that category again without knowing that its an article. For categories I am relying on the ContentNode base class and for Articles on the IUrlNode interface.

Category retrieval works fine and with a single query but after I actually get the category I have to use reflection to get the navigation property pointed by the RouteChild attribute to find that single article that matches my criteria. Problem is that the navigation property type is ICollection which means that it will at best use lazy loading and will bring all the articles from the database and will find the one I am looking for in memory.

My problem is also described in this previous post (not by me):

Entity Framework Code First IQueryable

Is there a way to have that navigation property as IQueryable or some other design that can go around this limitation?

1
  • Could you create a new queryable instead? i.e. Start a new query against the context using the Article's ID, from which you can navigate? Hope that makes sense... Commented Sep 26, 2011 at 8:13

1 Answer 1

5

No there is no way to have navigation property as IQueryable but you can change the collection to IQueryable by using:

IQueryable<Article> query = context.Entry(category).Collection(c => c.articles).Query();
query.Where(...).Load();

Generally your "algorithm" looks pretty strange. You want to work with base class but in the same time you want to access child properties. That sounds wrong and it can most probably be solved in better way (non "generic" way is also better).

Sign up to request clarification or add additional context in comments.

2 Comments

I will take a look at your suggestion. The whole point of this is to be able to have nodes on a tree of different types. A category is one of those types. I am breaking the tree and putting articles outside of it because I expect to have lots of them.
Yep worked great! Also found ObjectContext.GetObjectType really usefull. Thanks!

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.