query:
using(var ctxt = new NWDataContext())
{
var products = ctxt.Products.Where(p => p.ProductName.StartsWith("S"));
foreach(var v in products)
{
Console.WriteLine("{0} {1} {2}", v.ProductName, v.CategoryId.Value, v.OrderDetails.Count());
}
Console.WriteLine(ctxt.Products.Count());
}
Exception:
Exception: System.Data.EntityCommandExecutionException
Message: An error occurred while executing the command definition. See the inner exception for details.
Stacktrace:
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.Execute(MergeOption mergeOption)
at System.Data.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption)
at System.Data.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption)
at System.Data.Objects.DataClasses.RelatedEnd.Load()
at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad()
at System.Data.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject)
at System.Data.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item)
at System.Data.Entity.DynamicProxies.Product_C0742AFF5E64326E56801688B56EA2BB4A6C059ADFC9C7877F55A10DB2EC4BAE.get_OrderDetails()
at Tester.Program.Main(String[] args) in c:\Temp\generatortest\test1\Tester\Program.cs:line 24
Exception: System.InvalidOperationException
Message: There is already an open DataReader associated with this Command which must be closed first.
Stacktrace:
at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
Seems to occur at the first loop, on the query which pulls the order details:
SELECT [Extent1].[Discount] AS [Discount],
[Extent1].[OrderID] AS [OrderID],
[Extent1].[ProductID] AS [ProductID],
[Extent1].[Quantity] AS [Quantity],
[Extent1].[UnitPrice] AS [UnitPrice]
FROM [dbo].[Order Details] AS [Extent1]
WHERE [Extent1].[ProductID] = @EntityKeyValue1
I've attached the solution I used for testing which gave this exception.
Comments: In case this serves as a clarification: We understand for customers that hit this issue (e.g. because they are using SqlClient without MARS enabled or because they are using PostreSQL or some other provider or database backend that has the same limitation) are going to have a difficult time correlating the exception message with the workaround, and that is not great, but on the other hand we haven’t been able to come up with a general solution that doesn’t clash with some fundamental design assumptions, e.g.: - Queries are streaming and results are materialized on demand - There is a single database connection per context - EF’s provider model doesn’t contemplate a way to reason about provider-specific exceptions in a uniform way I would be great to hear if you have any specific suggestions on how to change EF to handle this condition better.
using(var ctxt = new NWDataContext())
{
var products = ctxt.Products.Where(p => p.ProductName.StartsWith("S"));
foreach(var v in products)
{
Console.WriteLine("{0} {1} {2}", v.ProductName, v.CategoryId.Value, v.OrderDetails.Count());
}
Console.WriteLine(ctxt.Products.Count());
}
Exception:
Exception: System.Data.EntityCommandExecutionException
Message: An error occurred while executing the command definition. See the inner exception for details.
Stacktrace:
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.Execute(MergeOption mergeOption)
at System.Data.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption)
at System.Data.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption)
at System.Data.Objects.DataClasses.RelatedEnd.Load()
at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad()
at System.Data.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject)
at System.Data.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item)
at System.Data.Entity.DynamicProxies.Product_C0742AFF5E64326E56801688B56EA2BB4A6C059ADFC9C7877F55A10DB2EC4BAE.get_OrderDetails()
at Tester.Program.Main(String[] args) in c:\Temp\generatortest\test1\Tester\Program.cs:line 24
Exception: System.InvalidOperationException
Message: There is already an open DataReader associated with this Command which must be closed first.
Stacktrace:
at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
Seems to occur at the first loop, on the query which pulls the order details:
SELECT [Extent1].[Discount] AS [Discount],
[Extent1].[OrderID] AS [OrderID],
[Extent1].[ProductID] AS [ProductID],
[Extent1].[Quantity] AS [Quantity],
[Extent1].[UnitPrice] AS [UnitPrice]
FROM [dbo].[Order Details] AS [Extent1]
WHERE [Extent1].[ProductID] = @EntityKeyValue1
I've attached the solution I used for testing which gave this exception.
Comments: In case this serves as a clarification: We understand for customers that hit this issue (e.g. because they are using SqlClient without MARS enabled or because they are using PostreSQL or some other provider or database backend that has the same limitation) are going to have a difficult time correlating the exception message with the workaround, and that is not great, but on the other hand we haven’t been able to come up with a general solution that doesn’t clash with some fundamental design assumptions, e.g.: - Queries are streaming and results are materialized on demand - There is a single database connection per context - EF’s provider model doesn’t contemplate a way to reason about provider-specific exceptions in a uniform way I would be great to hear if you have any specific suggestions on how to change EF to handle this condition better.