禁用 LINQ 上下文的所有延迟加载或强制预先加载
我有一个文档生成器,目前包含约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要设置
DeferredLoadingEnabled
,然后使用一些反射来包含每个属性,例如:
You will need to set
DeferredLoadingEnabled
, and then include every property using some reflection like:这对于 LINQ to SQL 来说很棘手。简短的回答是,这取决于。
如果您的实体的布局方式使您具有反映此关系的关系:
Customers ->订单-> OrderDetails
并且您需要评估所有 3 个实体的属性才能做出决定,最好的选择是编写联接。使用
.LoadWith
将使用单个语句获取Customers
和Orders
,但随后将对每个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 fetchCustomers
andOrders
using a single statement, but then will issue a query for every singleOrderDetails
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.