我在Windows 7 x64上使用MySql 5.1.53和MySql Connector / Net 6.4.4进行Visual Studio 2008 C#.NET 3.5项目。在我的应用程序中,我将我的数据库查询为:
IQueryable<Zoo> my_query = from d in my_context_.MySet
.Include("Foo")
.Include("Bar")
where d.Fuzz.Status == (int)Status.Pending
orderby d.Order
select d;
foreach (Zoo z in my_query)
{
if (some_expression)
{
// Lazy load some more data from the query. This throws a
// MySqlException
z.Something.Load();
}
else
{
// Lazy load some other data from the query. This also throws a
// MySqlException
z.SomethingElse.Load();
}
}
我得到的例外是:
System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.
我真的更喜欢能够延迟加载我的对象的其余部分,因为它需要它的元素。有没有办法做到这一点,或者我是否需要.Include()我的整个对象在原始查询中?
答案 0 :(得分:2)
不幸的是,MySQL不支持像SQL Server这样的多个活动结果集(MARS)。我不认为您的查询的EF实现会允许这样做。
但是,您可以创建一个类型为Task的Lazy泛型,它具有执行LINQ查询到EF的工厂操作。
答案 1 :(得分:1)
根据您获得的错误,您似乎仍然打开了用于从初始查询加载数据的连接,因此您无法在同一上下文中打开另一个连接。
首先尝试实现您的查询,这样当您仍在加载初始批量数据时,您不会尝试延迟加载:
var list = my_query.ToList();
foreach (Zoo z in list)
...