0

After getting this error :

MySqlException: There is already an open DataReader associated with this Connection which must be closed first.

I was unable to request or get result sets because i was querying while EF was still lazy loading other stuff that i had previously requested.

Found many possible solutions to address this issue which i have shared as an answer below.

MDT
  • 1,535
  • 3
  • 16
  • 29

1 Answers1

1

If you don't mention the type loading in EF configuration, EF will by default use Lazy Loading.

There are various ways to over come the 'Connection is open issue':

  1. By adding MARS to your EF connection string, please also read this before jumping into it.
  2. Use 'USING' statement, but for this you need to create a new entity object every time it gets disposed.
  3. Convert your result to Generics types or into local object types in my case i converted it ToList() which helped address my issue and i was able to request a new result set from the context.

I have a base class that provides me with the context object, which is why i didn't use Using statement to create new context every-time i wanted to query the context.

feel free to edit any mistakes, still learning about EF and its behavior.

MDT
  • 1,535
  • 3
  • 16
  • 29