I'm confused to why this is happening. I'm new to LINQ so I'm clearly missing something here, that is probably pretty easy. I've looked up help on the topic, but I don't really know what to ask so I haven't found any answers that really address my question.
This doesn't work
It throws an EntityCommandExecutionException
when the FirstOrDefault
method is executed.
var query = from band in context.BandsEntitySet
where band.ID == 12345
select band;
foreach (var item in query)
{
string venueName = item.VenueName;
var venue = context.VenueEntitySet.FirstOrDefault(r => r.Venue.Equals(venueName));
if(venue != null)
{
Debug.WriteLine(item.Name + " is playing in " + venueName + " on the " + item.PlayDate);
Debug.WriteLine("The address of " + venueName + " is " + venue.Address);
}
}
This works
var query = from band in context.BandsEntitySet
where band.ID == 12345
select band;
var bandList = query.toList();
foreach (var item in bandList)
{
string venueName = item.VenueName;
var venue = context.VenueEntitySet.FirstOrDefault(r => r.Venue.Equals(venueName));
if(venue != null)
{
Debug.WriteLine(item.Name + " is playing in " + venueName + " on the " + item.PlayDate);
Debug.WriteLine("The address of " + venueName + " is " + venue.Address);
}
}
My question is simple: Why is the exception being thrown? And why does creating a list from the query allow me to use the FirstOrDefault
method?
Exception Message: A first chance exception of type 'System.Data.EntityCommandExecutionException' occurred in System.Data.Entity.dll
I guess I am wrong in my assumption that query is a list? Then what is it exactly?
Here is the stack trace
A first chance exception of type 'System.Data.EntityCommandExecutionException' occurred in System.Data.Entity.dll
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence)
at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
at BandManagementProject.AutoUpdate.Dev() in c:\BandManagementProject\AutoUpdate.cs:line 99
at BandManagementProject.AutoUpdate.Main(String[] args) in c:\BandManagementProject\AutoUpdate.cs:line 41
Inner Exception
MySql.Data.MySqlClient.MySqlException (0x80004005): There is already an open DataReader associated with this Connection which must be closed first.
at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
at MySql.Data.MySqlClient.MySqlCommand.Throw(Exception ex)
at MySql.Data.MySqlClient.MySqlCommand.CheckState()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
Conclusion I didn't close my LINQ query before executing additional queries. Didn't realize I needed to do that. I appreciate all the help!
thanks, Justin