禁用所有延迟加载或强制预先加载了LINQ上下文 [英] Disable all lazy loading or force eager loading for a LINQ context

查看:133
本文介绍了禁用所有延迟加载或强制预先加载了LINQ上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件生成器,其中包含用于查询此刻的约200项,但很可能会在500以上时完成。我最近注意到一些映射表示延迟加载。这为它需要访问的基础上,正在生成该文件的所有这些属性的文档生成器有问题。

I have a document generator which contains queries for about 200 items at the moment but will likely be upwards of 500 when complete. I've recently noticed that some of the mappings denote lazy loading. This presents a problem for the document generator as it needs access to all of these properties based on which document is being generated.

虽然我知道的 DataLoadOptions 可指定的范围内,这将导致我不得不明确指定每一个也可能会被加载列。这是1000北部,因为它所有的数据抓取的发生在一个上下文。

While I am aware of the DataLoadOptions that can be specified to the context, this would result in me having to explicitly specify every column that could possibly be loaded. That is north of 1000 as it all of the data fetching takes place in one context.

有没有办法对我来说,禁用延迟加载上下文或明确启用eager装载忽略延迟加载的属性?也许扩展DB上下文类和压倒一切的东西。

Is there any way for me to disable lazy loading for a context or explicitly enable eager loading to ignore the defer loading property? Perhaps extending the DB context class and overriding something?

推荐答案

您需要设置的 DeferredLoadingEnabled ,然后包括使用一些每个属性像反思:

You will need to set DeferredLoadingEnabled, and then include every property using some reflection like:

DataLoadOptions dataLoadOptions = new DataLoadOptions();

foreach (PropertyInfo pi in typeof(SomeThingyClass).GetProperties())
{
    ParameterExpression paramExp = Expression.Parameter(typeof(SomeThingyClass), "s");
    Expression expr = Expression.Convert(Expression.Property(paramExp, pi.Name), typeof(object));
    LambdaExpression lambda = Expression.Lambda(expr, paramExp);
    dataLoadOptions.LoadWith((Expression<Func<SomeThingyClass, object>>) lambda);
}

这篇关于禁用所有延迟加载或强制预先加载了LINQ上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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