I have a Visual Studio 2008 C# .NET 3.5 project using MySql 5.1.53 and MySql Connector/Net 6.4.4 on Windows 7 x64. In my application, I query the my database as:
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();
}
}
The exception I get is:
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.
I would really prefer to be able to lazy-load the rest of my object as its elements are needed. Is there a way to do this, or do I need to .Include() my entire object in the original query?
Based on the error you’re getting, you appear to still have the connection open that is being used to load in data from the initial query, so you can’t open another connection on the same context.
Try materializing your query first, so you are not trying to lazy load while you are still loading the initial batch of data in: