question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"There is already an open DataReader associated with this Connection..." on .Include()

See original GitHub issue

The issue

I’m trying to load multiple pieces of data, and my second piece of data is dependent on the first piece of data. Basically I don’t know whether I need to load the second piece of data, before I retrieved the first piece of data.

Here’s an example:

// First piece of data
var frontPageSettings = _context.Settings.FirstOrDefault(x => x.Key == Constants.SettingsKeys.FrontPageSettings.ToString());
if (frontPageSettings != null && frontPageSettings.ShowFeaturedThreads)
{
    var threads = _context.Threads.Include(x => x.UserProfile).Where(x => x.IsFeatured).OrderByDescending(x => x.FeatureDate).Take(3).ToList();
}

If I remove the .Include(x=> x.UserProfile) I’m not getting any exceptions, so I believe that’s the source of the problem.

This creates the following exception:

Exception message: There is already an open DataReader associated with this Connection which must be closed first.
Stack trace:
   at MySql.Data.MySqlClient.MySqlCommand.VerifyValid()
   at MySql.Data.MySqlClient.CommandExecutors.TextCommandExecutor.<ExecuteReaderAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.CommandExecutors.TextCommandExecutor.<ExecuteNonQueryAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.MySqlCommand.<ExecuteNonQueryAsync>d__46.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at MySql.Data.MySqlClient.MySqlTransaction.Dispose(Boolean disposing)
   at Microsoft.EntityFrameworkCore.Storage.Internal.MySqlRelationalTransaction.Dispose()
   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_Include>d__30`1.System.IDisposable.Dispose()
   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_Include>d__30`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at OurGhostsOfWar.Controllers.HomeController.Index()

Further technical details

Pomelo.EntityFrameworkCore.MySql version: 1.1.1-prerelease-10017

Other details about my project setup: I’m using MAriaDB instead of MySQL, but that’s just a simple fork of MySQL. Shouldn’t cause any problems problems, and haven’t the during the last year I’ve been working with MariaDB and the Pomelo.EntityFrameworkCore.MySql package provider

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
caleblloydcommented, Apr 26, 2017

I’ll look into the incorrect exception. It comes because the reader hasn’t been used due to the first exception, then the transaction getting closed throws the DataReader exception. We need to make sure the first exception gets through though.

0reactions
detiliumcommented, Apr 26, 2017

Found my issue.

I’m not sure why it’s giving me the exception: There is already an open DataReader associated with this Connection which must be closed first., when the actual problem is an InvalidCastException.

I had a datatype of BLOB in my DB, but in my class i defined the property as a string. It tried to cast a byte[] into a string, hence the invalid cast exception.

Thank you for you help anyways, at least I got my package updated 👍 I hope I didn’t take to much of your time 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

There is already an open DataReader associated with this ...
There is already an open DataReader associated with this Connection which must be closed first. I am using Visual Studio 2010/.Net 4.0 and...
Read more >
There is already an open DataReader associated with this ...
You have to close SqlDataReader when you finished using the it.When SqlDataReader is open, the Connection is in use by it.Hence you cannot ......
Read more >
There is already an open DataReader associated with this ...
This is valid error. You cannot issue another query over connection, while previous one is still running. You can fix it by fetching...
Read more >
Script Task: There is already an open DataReader ...
' A Microsoft platform for building enterprise-level data integration and data transformations solutions.
Read more >
There Is Already an Open DataReader Associated With ...
There is already an open DataReader associated with this command which must be closed first. is a common error. This guide teaches how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found