实体框架4:多对多关系IQueryable而不是ICollection [英] Entity Framework 4: Many to Many relationship IQueryable instead of ICollection

查看:166
本文介绍了实体框架4:多对多关系IQueryable而不是ICollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家早上好,

我正在尝试解决一个遇到EF代码的问题。我的模式是以下

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

我已经编写了代码,我可以从中检索类别数据库,而不知道它的一个类别。从那时起,我必须在不知道它的文章的情况下再次检索该类别的单个文章。对于类别,我依赖于ContentNode基类和IUrlNode接口上的文章。

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.

类别检索工作正常,使用单个查询,但在实际获取类别后,我必须使用反射来获取RouteChild属性指向的导航属性,以查找该单个符合我标准的文章问题是导航属性类型是ICollection,这意味着它将最好使用延迟加载,并将带来数据库中的所有文章,并找到我正在寻找的内存。

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

实体框架代码首先是IQueryable

有没有办法将该导航属性设置为IQueryable或某些可以解决这个限制的其他设计?

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

推荐答案

没有办法将导航属性设置为 IQueryable ,但您可以使用以下方式将集合更改为 IQueryable

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

这篇关于实体框架4:多对多关系IQueryable而不是ICollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆