Entity Framework with SQL Azure, "There is already an open DataReader associated with this Command which must be closed first."}

I hit this error when I deployed an app built with Entity Framework onto Windows Azure.  Obviously, I wasn’t explicitly using DataReader so it kind of puzzled me.  As it happens every time Smile It worked well with local database, application running local and database on SQL Azure.  But once I used the Dev Fabric to test the application locally, it failed and gave the above error.

Entity Framework relies on using Multiple Active Results Sets (MARS), and it would be a part of connection string multipleactiveresultsets=True.  Earlier SQL Azure didn’t  support MARS, so we have to explicitly set it to “false” before deploying to SQL Azure.

But starting April 2010, SQL Azure supports MARS (yeah, that is the level of update I had on SQL Azure i..e 1 year late Smile) and this post nicely covers on this particular topic http://blogs.msdn.com/b/adonet/archive/2010/06/09/remember-to-re-enable-mars-in-your-sql-azure-based-ef-apps.aspx

On the new SQL Azure features, read http://blogs.msdn.com/b/sqlazure/archive/2010/04/16/9997517.aspx 

In my case, I wired up the connection string myself using the Azure user account and password and totally forgot the MARS setting.

All I had to do was to add the multipleactiveresultsets=True to my connection string and it started working fine.

If you surface this error even after adding the above, and find some resolution, do post in the comments Smile


Print | posted on Tuesday, June 14, 2011 12:51 PM

Comments on this post

No comments posted yet.

Your comment:

 (will show your gravatar)