So this is one of those blog posts that I felt the need to write to remind myself in the future of the weirdness I am experiencing.

I am currently working on an application that will be hosted on Azure and I have my development environment locally and a staging site on Azure.

Today I had one of those cases where it all worked fine locally, both in the IDE and accessing the site through full IIS, yet when I deployed to cloud I got a big fat error.

Here’s the stack trace for those that are interested and of course so google can index it and help someone else with the same issue find this post.



Stack Trace

[EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details.]

System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior):41

System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.ExecuteTResultType:311

System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransactionT:150

System.Data.Entity.Core.Objects.ObjectQuery`1+<>cDisplayClass7.b5():59

System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.ExecuteTResult:56

System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption):165

System.Data.Entity.Core.Objects.DataClasses.EntityCollection1.Load(List1 collection, MergeOption mergeOption):41

System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad():216

System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadPropertyTItem:55

System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior+<>cDisplayClass7`2.b1(TProxy proxy, TItem item):56

System.Data.Entity.DynamicProxies.Booking9A66496855F1C80DA6341F7F554CEAB46111699BB75AD4A2282C0D94D46E9D35.getBookingLines():7

FroidPublic.Controllers.FroidBaseController.LoadHoomanBookings(Int32 bookingState, Int32 id):309

FroidPublic.Controllers.FroidBaseController.GetHooman(String email):542

FroidPublic.Controllers.HoomanController.Index():17

[InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.]

System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command):54

System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async):149

System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite):39

System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method):16

System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method):62

System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior):23

System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func3 operation, TInterceptionContext interceptionContext, Action3 executing, Action3 executed):23

System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext):129

System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior):17

Thanks to [RayGun](http://raygun.io) it was easy to track down the error but I am still not sure why it’s occurring.
This is the line of code that was causing the error:
var items = (from b in DataContext.Bookings where (b.BookingStateId & bookingState) > 0 && b.CarerId id orderby b.CheckIn ascending select b);
I was then iterating over the results and for some reason when hosted on Azure it resulted in [InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.]
The exact same code running locally against SQL 2012 worked! My connection string allows multiple active result sets.
I suspect it is this line that was actually causing the error:
var service = (from b in item.BookingLines where b.CarerService.ShowAsService
true select b).First();
To fix the issue all I needed to do was add a ToList() on the items returned from the query like so:
var items = (from b in DataContext.Bookings where (b.BookingStateId & bookingState) > 0 && b.CarerId == id orderby b.CheckIn ascending select b).ToList();
Strange but true