禁用 LINQ 上下文的所有延迟加载或强制预先加载

发布于 2024-09-12 10:42:33 字数 258 浏览 1 评论 0原文

我有一个文档生成器,目前包含约 200 个项目的查询,但完成后可能会超过 500 个。我最近注意到一些映射表示延迟加载。这给文档生成器带来了一个问题,因为它需要根据生成的文档来访问所有这些属性。

虽然我知道可以指定给上下文的 DataLoadOptions,但这将导致我必须显式指定可能加载的每一列。这超出了 1000,因为所有数据获取都发生在一个上下文中。

有什么方法可以禁用上下文的延迟加载或显式启用急切加载以忽略延迟加载属性?也许扩展数据库上下文类并覆盖某些东西?

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.

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.

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?

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

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

发布评论

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

评论(2

揽清风入怀 2024-09-19 10:42:33

您需要设置DeferredLoadingEnabled,然后使用一些反射来包含每个属性,例如:

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

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);
}
寒冷纷飞旳雪 2024-09-19 10:42:33

这对于 LINQ to SQL 来说很棘手。简短的回答是,这取决于。

如果您的实体的布局方式使您具有反映此关系的关系:

Customers ->订单-> OrderDetails

并且您需要评估所有 3 个实体的属性才能做出决定,最好的选择是编写联接。使用 .LoadWith 将使用单个语句获取 CustomersOrders,但随后将对每个 OrderDetails 发出查询> 也记录一下。

因此,即使您确实使用 LoadWith 指定了每个子关系,您也不会发出单个查询来检索结果。

This is tricky with LINQ to SQL. The short answer is, it depends.

If your entities are laid out in a manner such that you have a relationship that mirrors this:

Customers -> Orders -> OrderDetails

And you need to evaluate properties on all 3 entities in order to make a decision, your best bet is to go with writing a join. Using .LoadWith will fetch Customers and Orders using a single statement, but then will issue a query for every single OrderDetails record as well.

So, even if you did specify every child relationship with LoadWith, you're not going to get a single query issued to retrieve the result.

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