使用ObjectContext.CreateQuery(实体框架和存储库)加载相关实体(Eager Load)时遇到麻烦 [英] Having troubles loading related entities (Eager Load) with ObjectContext.CreateQuery (Entity Framework and Repositories)

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

问题描述

这是一些我尝试过的东西...希望你可以推断出我正在努力做什么,做错什么。好的,所以我在使用这个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);
   }

如果我只是使用这个,我最终得到一个包含所有适当的参数,除了那些相关实体... ie。如果一个是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);
}

但是没有工作...

如果我没有正确的轨道,请让我知道。有没有人知道如何在使用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.

谢谢,
Matt

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是一个写入的方法,它返回字符串实体集您的实体类型的名称,或使用渴望加载策略

...where GetEntitySetName is a method you write which returns the string entity set name for your entity type, or use an eager loading strategy.

为什么选择OFTYPE?如果您在模型中继承,则此ESQL将返回 ObjectQuery< TParent> 而不是 ObjectQuery< TChild>

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

最后,如果实体E具有属性命名, Include 用户。类型和实体集名称与 Include 无关

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(实体框架和存储库)加载相关实体(Eager Load)时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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