LINQ2SQL贪婪加载多个DataLoadOptions [英] Linq2SQl eager load with multiple DataLoadOptions

查看:211
本文介绍了LINQ2SQL贪婪加载多个DataLoadOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想取使用LINQ2SQL渴望加载数据。在code是相似的:

I like to fetch the data with eager-loading using Linq2SQL. The code is similar as :

       DataLoadOptions options = new DataLoadOptions();

       options.LoadWith<Product>(c => c.ProductCompanies);

       options.LoadWith<Product>(c => c.OrderDetails);

       db.LoadOptions = options;

       IEnumerable<Product> products = db.Products.ToList<Product>();

我检查了超过1 SQL查询如我所料。实际上它只能做急​​切装载有产品和订单明细,并且ProductCompany查询一个接一个。难道我做错什么吗?或者它是一个LINQ2SQL的问题?难道我们有什么解决方法吗?

I check it generated more than 1 SQL query as I expected. Actually it only do eager-loading with Product and OrderDetails, and the ProductCompany is queried one by one. Did I do anything wrong here? Or it is a Linq2SQL issue? Do we have any workaround?

非常感谢!

更新: 我检查从SQL事件探查器的SQL。我发现这两个Leppie和伊恩是正确的。它们是有界在一个事务中。但是,当我将它设置为延迟加载,它打开了多个连接。

Update: I check the sql from SQL Profiler. I found both Leppie and Ian are correct. They are bounded in one transaction. But when I set it as lazy load, it opened multiple connection.

推荐答案

没有,你没有做错什么,LINQ2SQL批处理,一切都在一个单一的交易,但可能执行查询的无限数量所需的结果。 DataLoadOptions 通常仅用于当的DataContext 不适用于生成的使用的整个背景。如果你能保持在的DataContext 执行过程中存活,最好是依靠​​延迟执行(即默认值)。

No, you didn't do anything wrong, Linq2SQL batches everything in a single transaction, but might execute an unbounded number of queries for the required result. DataLoadOptions is normally only used when the DataContext is not available for the entire context of the resulting usage. If you can keep the DataContext alive during execution, it is best to rely on deferred execution (that is default).

这篇关于LINQ2SQL贪婪加载多个DataLoadOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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