在遇到问题时加载相关实体(贪婪加载)与ObjectContext.CreateQuery(实体框架和库) [英] Having troubles loading related entities (Eager Load) with ObjectContext.CreateQuery (Entity Framework and Repositories)

查看:121
本文介绍了在遇到问题时加载相关实体(贪婪加载)与ObjectContext.CreateQuery(实体框架和库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一堆东西我想...希望你可以从中推断我想要做什么,我做错了。好了,所以用这个DoQuery当我在与装载相关实体的问题:

Here's a bunch of things I tried... hopefully you can extrapolate from it what I'm trying to do and what I'm doing wrong. Okay so I'm having problems with loading related entities when using this DoQuery:

   public ObjectQuery<E> DoQuery(ISpecification<E> where)
   {
        return (ObjectQuery<E>)_ctx.CreateQuery<E>("[" + typeof(E).Name + "]").Where(where.EvalPredicate);
   }

如果我只是用这个,我最终得到包含除了相关实体...即那些在所有适当的参数对象回来。如果是的引用到用户表中的用户名,我不回去User对象也是如此。

If I just use this, I end up getting an object back that contains all proper parameters except the ones that are related entities... ie. if one is a UserID that's referenced to the User table, I do not get back the User object as well.

我看过了,你可以做一个.INCLUDE(用户)做实体的贪婪加载...但是当我尝试这样它不工作:

I read up that you can do a .Include("User") to do an Eager Load of the entity... but it doesn't work when I try this:

public ObjectQuery<E> DoQuery(ISpecification<E> where)
{
     return (ObjectQuery<E>)_ctx.CreateQuery<E>("[" + typeof(E).Name + "]").Include("User").Where(where.EvalPredicate);
}

我还检查以确保实体集的名称和型号名是用户,他们所。我能想到的唯一的其他事情是把多东西,在([+ typeof运算(E).Name点+]),但我不知道如何将在那里多个实体......这里是因为我看到有人说,你可以通过把一个加载多个是我的尝试。插图中。

I also checked to make sure the Entity Set Name and model name are "User", which they are. The only other thing I could think of is to put multiple things in the ("[" + typeof(E).Name + "]") but I'm not sure how to include multiple entities in there... Here's what I tried since I saw someone said you could load multiple by putting a . inbetween.

public ObjectQuery<E> DoQuery(ISpecification<E> where)
{
     return (ObjectQuery<E>)_ctx.CreateQuery<E>("[" + typeof(E).Name + "].[User]").Where(where.EvalPredicate);
}

但是,没有工作......

But that didn't work...

如果我没有在正确的轨道上,请让我知道。有谁知道如何使用ObjectContext.CreateQuery时加载相关实体?任何建议或洞察力帮助。

If I'm not on the right track please let me know. Does anyone know how to load the related entities when using ObjectContext.CreateQuery? Any suggestions or insight helps.

谢谢,

马特

Thanks,
Matt

推荐答案

的createQuery需要一个ESQL声明。所以,你可以写这样的:

CreateQuery takes an ESQL statement. So you can write something like:

public ObjectQuery<E> DoQuery(ISpecification<E> where)
{
    var esql = String.Concat(
         "SELECT VALUE e1 FROM OFTYPE(", 
         GetEntitySetName(typeof(E)),
         ", ", 
         typeof(T).FullName, 
         ") AS e1");
    return Context.CreateQuery<T>(esql).Include("User").Where(where.EvalPredicate);
}

...其中GetEntitySetName是你写这为您的实体类型返回字符串实体集名称,或者使用<一的方法href=\"http://blogs.msdn.com/alexj/archive/2009/07/25/tip-28-how-to-implement-include-strategies.aspx\"相对=nofollow>预先加载策略。

为什么OFTYPE?如果你在你的模型有继承,这ESQL将返回的ObjectQuery&LT; TParent&GT; 而不是的ObjectQuery&LT;年掳&GT; 没有它

Why OFTYPE? If you have inheritance in your model, this ESQL would return ObjectQuery<TParent> instead of ObjectQuery<TChild> without it.

最后,包含只能如果实体E具有的属性指定的用户。类型和实体集的名字无关的包含

Finally, the Include only works if the entity E has a property named User. The type and entity set name are irrelevant to Include.

这篇关于在遇到问题时加载相关实体(贪婪加载)与ObjectContext.CreateQuery(实体框架和库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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