当我不知道要加载的属性的名称时,如何进行急切加载?

发布于 2024-08-15 17:41:05 字数 947 浏览 2 评论 0原文

我有一个通用存储库,当我使用 DoQuery 方法从数据库中选择对象时,我需要加载一些相关实体,以免在外键字段的位置出现空值。

问题是存储库是通用的,所以我不知道需要加载多少属性或它们的名称是什么(除非有某种方法获取它们)我怎样才能使所有外键在加载时加载它们的实体仍然保持这个存储库通用吗?

这是 DoQuery 方法:

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

以及指向整个存储库的原始代码的链接。

我之前发布过一次,但从未得到答案,但我认为这个问题更相关,因为在人们假设我知道属性名称并且可以做到这一点之前:

.Include("PropertyNameIKnowNeedsToBeLoaded")

这是我之前发布的问题希望这能提供一些关于我在这方面的进展的信息。

任何帮助表示赞赏。

谢谢,
马特

I have a generic repository and when I'm using a DoQuery method to select objects from the database, I need to load some of the related entities in order to not get nulls in the place of the fields that are foreign keys.

The problem is that the repository is generic, so I do not know how many properties need loading or what their names are (unless there's some way of getting them) how can I make it so that all of the foreign keys have their entities loaded while still keeping this repository generic?

Here's the DoQuery method:

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

And a link to the original code for the entire repository.

I posted this once before and never got the answer to it but I figure this one is a little bit more relevant since before people were assuming I knew the property names and could do:

.Include("PropertyNameIKnowNeedsToBeLoaded")

Here's the question I posted before hopefully that will provide a little information on where I'm at with this.

Any help is appreciated.

Thanks,
Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海螺姑娘 2024-08-22 17:41:05

您必须通过将应获取的深度传递给方法来反转控制。我对实体框架不够熟悉,也不知道如何做到这一点。您可以传入一个表示获取深度的整数,该方法可以计算出如何使用该深度的相关实体构建查询。

You'll have to invert the control by passing into the method the depth that should be fetched to. I'm not familiar enough with the Entity framework with exactly how to do that. You could pass in an integer representing the fetch depth, and the method can figure out how to construct a query with the related entities to that depth.

剩一世无双 2024-08-22 17:41:05

我已经通过下一个方法解决了这个问题:

 public IQueryable<E> GetAll()
        {
            Type et=typeof(E);
            ObjectQuery<E> oq=_context.CreateQuery<E>("[" + typeof(E).Name + "Set]");

            foreach(System.Reflection.PropertyInfo pi in et.GetProperties())
            {
                if (pi.PropertyType.Name.Contains("EntityCollection"))
                        oq=oq.Include(pi.Name);

            }

            return oq.AsQueryable();
        }

但是它的钢有一个与铲斗关系的问题。所以钢在思考这个问题

I have solved this problem by the next way:

 public IQueryable<E> GetAll()
        {
            Type et=typeof(E);
            ObjectQuery<E> oq=_context.CreateQuery<E>("[" + typeof(E).Name + "Set]");

            foreach(System.Reflection.PropertyInfo pi in et.GetProperties())
            {
                if (pi.PropertyType.Name.Contains("EntityCollection"))
                        oq=oq.Include(pi.Name);

            }

            return oq.AsQueryable();
        }

but it steel has a problems with dipper relations. So steel thinking about this problem

狼性发作 2024-08-22 17:41:05

您可以获取类型的关系端名称,并为所有关系调用 Include。

    public ObjectQuery<E> DoQuery<E>(ISpecification<E> where) where E : new()
    {
        ObjectQuery<E> query = (ObjectQuery<E>)_ctx.CreateQuery<E>("[" + typeof(E).Name + "]").Where(where.EvalPredicate);

        IEntityWithRelationships entityWithRelationship = (IEntityWithRelationships)(new E());
        IEnumerable<IRelatedEnd> relatedEnds = entityWithRelationship.RelationshipManager.GetAllRelatedEnds();
        foreach (IRelatedEnd end in relatedEnds)
        {
            query= query.Include(end.TargetRoleName);
        }

        return query;
    }

我添加了一个“新”通用约束,因为我需要从查询类型获取 IEntityWithRelationships。我希望这有帮助。

You can get the relations ends names for a type and call Include for all relations.

    public ObjectQuery<E> DoQuery<E>(ISpecification<E> where) where E : new()
    {
        ObjectQuery<E> query = (ObjectQuery<E>)_ctx.CreateQuery<E>("[" + typeof(E).Name + "]").Where(where.EvalPredicate);

        IEntityWithRelationships entityWithRelationship = (IEntityWithRelationships)(new E());
        IEnumerable<IRelatedEnd> relatedEnds = entityWithRelationship.RelationshipManager.GetAllRelatedEnds();
        foreach (IRelatedEnd end in relatedEnds)
        {
            query= query.Include(end.TargetRoleName);
        }

        return query;
    }

I added a "new" generic constraint because I need to get an IEntityWithRelationships from the queried type. I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文